我展示的 UIViewController 有一个 UIImageView,即 backgroundImageView
,作为 VC 的背景并设置为填充整个屏幕。
我希望 UIImageView 呈现 iOS 提供的深色模糊效果,但当我第一次呈现 View Controller 时,会暂时出现以下尺寸问题(在设备上,但不在模拟器上):
内部viewDidLoad()
:
// `locationImage` is passed during the segue
backgroundImageView.image = locationImage
backgroundImageView.contentMode = .scaleAspectFill
let blurEffect = UIBlurEffect(style: .dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = backgroundImageView.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
backgroundImageView.addSubview(blurEffectView)
上述 View Controller 的modalPresentationStyle
设置为currentContext
。
如果我暂时删除模糊效果,我会发现我给 View Controller 的背景图像已正确设置。然而,一旦应用模糊效果,尺寸问题就会立即出现。
到底是什么原因造成的?
最佳答案
尝试在viewWillLayoutSubviews()
或viewDidLayoutSuviews()
中设置blurEffectView的框架
here您可以找到有关 viewcontroller 生命周期的更多信息
关于ios - 模糊的 UIImageView 尺寸问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44421136/