padarom
padarom

Reputation: 3648

Adding vibrancy to existing view

I am rather new to Swift and currently trying to achieve a modal with a Blur and Vibrancy effect.

let modalViewController = FavoritesViewController()
modalViewController.modalPresentationStyle = .OverFullScreen
self.presentViewController(modalViewController, animated: true, completion: nil)

This is how I create the Modal View Controller, which then executes the following viewDidLoad() method:

override func viewDidLoad() {
    super.viewDidLoad()

    // Instantiate a view from the FavoritesView.xib and add
    // it to this Controller's view
    let optionsView = UINib(nibName: "FavoritesView", bundle: nil).instantiateWithOwner(self, options: nil)[0] as UIView
    view.addSubview(optionsView)

    // Create a UIVisualEffectView with a blur effect and
    // insert it to this Controller's view
    let blurEffect = UIBlurEffect(style: .Dark)
    let blurView = UIVisualEffectView(effect: blurEffect)
    blurView.frame = self.view.bounds
    blurView.autoresizingMask = .FlexibleWidth | .FlexibleHeight
    blurView.setTranslatesAutoresizingMaskIntoConstraints(true)
    view.insertSubview(blurView, atIndex: 0)

    // Create a UIVisualEffectView with a vibrancy effect
    let vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(forBlurEffect: blurEffect))
    vibrancyView.setTranslatesAutoresizingMaskIntoConstraints(false)

    vibrancyView.contentView.addSubview(optionsView) 
    // This is the part I'm unsure of.

    blurView.contentView.addSubview(vibrancyView)
}

With this code the blur is created and shown, but the vibrancy doesn't seem to work. From what I understand, first optionsView (which is the modal view containing labels and buttons) is added to the Controller's view, then the blur is being inserted behind it.

Finally the Vibrancy View gets created, the optionsView gets added to the vibrancy view's contentView (I assume this is necessary, so that the vibrancy effect can get applied to the views labels and such). This Vibrancy View then gets added to the blurView.

However, labels and buttons still don't show up with the vibrancy effect applied. Do I need to make any adjustments to the UI elements in the .xib-file in order for the Vibrancy to be applied or should it be applied automatically? Or am I missing something else?

Upvotes: 0

Views: 586

Answers (1)

kurtanamo
kurtanamo

Reputation: 1828

Hi just try it on viewWillAppear

viewDidLoad isn't the right place for elements on screen (UIView and so on..)

Upvotes: 0

Related Questions