How to add a UIVisualEffectView using swift in iOS


This is a code to create a UIVisualEffectView using Swift Language in iOS

  
// Blur Effect
        var blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
        var blurEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView.frame = view.bounds
        view.addSubview(blurEffectView)
        
        
        // Vibrancy Effect
        var vibrancyEffect = UIVibrancyEffect(forBlurEffect: blurEffect)
        var vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)
        vibrancyEffectView.frame = view.bounds

Add a UILabel in subview of Visual Effect View

// Label for vibrant text
        var vibrantLabel = UILabel()
        vibrantLabel.text = "Swift"
        vibrantLabel.font = UIFont.systemFontOfSize(72.0)
        vibrantLabel.sizeToFit()
        vibrantLabel.center = view.center
       
        // Add label to the vibrancy view
        vibrancyEffectView.contentView.addSubview(vibrantLabel)
       
        // Add the vibrancy view to the blur view
        blurEffectView.contentView.addSubview(vibrancyEffectView)
UIVisualEffectView using swift
UIVisualEffectView using swift

Download Source code here

-How to make an iOS App
ktrkathir

Author: Kathiresan Murugan

Associate Technical Lead - iOS

Leave a comment