UIBlurEffectView 比自动布局的 parentView 小,尽管框架相同

UIBlurEffectView smaller than autolayouted parentView although identical frame

我在屏幕中间有一个用于登录的 UIView。它被限制在周围视图的0.25高度(覆盖整个window)

我注意到,如果我创建一个 UIVisualEffectView(通过方法 blurBackgroundForView(_) 作为 UIView 的背景,它太小了(检查我如何创建UIVisualEffectView) 尽管它具有相同的框架。 当你将 backgroundColor 更改为 .greenColor 时,你可以看到效果。 View比Blureeffect高

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)
}

viewWillAppear 中的自动布局无法保证设置框架。

尝试在 viewDidLayoutSubviews 中设置模糊视图的框架。或者,您可以直接在模糊视图上设置自动调整大小蒙版,以便在其父视图调整大小时调整大小:

blurView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

你还应该调用viewWillAppear中的super方法。