按钮时模糊效果

时间:2017-02-20 12:19:35

标签: ios swift3 blur

我想在viewcontroller的视图背景中使用模糊效果但有时它不起作用,当它工作时它将无法正常工作 当我在iPad等大型熨平板设备上运行程序时,顶部有一个黑色方块,我看不到模糊效果

这是我的代码

class sahafiViewController: UIViewController , UIImagePickerControllerDelegate , UINavigationControllerDelegate {




@IBOutlet weak var backgroundImg: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
    let blurView = UIVisualEffectView(effect: blurEffect)
    blurView.frame = backgroundImg.bounds
    backgroundImg.addSubview(blurView)


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}}

here is what happens

2 个答案:

答案 0 :(得分:0)

在viewDidAppear方法中尝试使用您的代码。

答案 1 :(得分:0)

您可以尝试以下代码:

覆盖func viewDidLoad(){

     super.viewDidLoad()

     let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
     let blurView = UIVisualEffectView(effect: blurEffect)

     blurView.frame =  CGRect(x: self. backgroundImg.frame.origin.x, y: self. backgroundImg.frame.origin.y, width: self. backgroundImg.frame.size.width, height: self. backgroundImg.frame.size.height)

     blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

     self.backgroundImg.addSubview(blurView)
}