如何在没有警告的情况下正确添加iOS 11状态栏上的模糊效果?

时间:2017-12-23 05:16:32

标签: ios swift ios11 statusbar

我曾经使用以下代码在状态栏上添加模糊效果:

// Add blur effect on status bar
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.extraLight)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(blurEffectView)
blurEffectView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
blurEffectView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
blurEffectView.topAnchor.constraint(equalTo: self.topLayoutGuide.topAnchor).isActive = true
blurEffectView.bottomAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor).isActive = true

自Xcode 6或7开始工作,即使对于Xcode 9也是如此。但是,自Xcode 9以来,最后两行开始发出警告:

'topLayoutGuide' was deprecated in iOS 11.0: Use view.safeAreaLayoutGuide.topAnchor instead of topLayoutGuide.bottomAnchor

通过关注警告,如果我将topLayoutGuide更改为建议值,则结果不正确(所有屏幕均为空白)。

如何在没有警告的情况下正确添加iOS 11状态栏上的模糊效果?

1 个答案:

答案 0 :(得分:1)

blurEffectView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
blurEffectView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
blurEffectView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
blurEffectView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true