0

I have an UIView for login purposes centered in the middle of the screen. It is constrained to a 0.25 height of the surrounding view (covering the whole window)

I noticed, that if I create an UIVisualEffectView (via the method blurBackgroundForView(_)as background for the UIView, that it is too small (check the code how I create the UIVisualEffectView) although it has the same frame. You can see the effect, when you change the backgroundColor to .greenColor. The View is higher than the Blureffect.

ViewController

override func viewWillAppear(animated: Bool) {
  AnimationHelper.blurBackgroundForView(self.view)
  view.backgroundColor = .greenColor()
}

blurBackgroundForView(_)

static func blurBackgroundForView(view: UIView!){
  view.backgroundColor = .clearColor()
  let blurEffect = UIBlurEffect(style: .Light)
  let blurEffectView = UIVisualEffectView(effect: blurEffect)
  blurEffectView.frame = view.bounds
  view.insertSubview(blurEffectView, atIndex: 0)
}

enter image description here

gutenmorgenuhu
  • 2,312
  • 1
  • 19
  • 33

1 Answers1

1

Frames are not guaranteed to be set by auto layout in viewWillAppear.

Try setting the blur view's frame in viewDidLayoutSubviews instead. Alternatively, you can directly set the autoresizing mask on your blur view so that it resizes when its superview resizes:

blurView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

You should also call the super method in viewWillAppear.

bdrell
  • 194
  • 2
  • 5
  • Thank you for your answer. Calling it from `viewDidLayoutSubviews` did not change anything, but the setting the autoresizingMask did the trick. – gutenmorgenuhu May 22 '15 at 19:58