如何在其上显示视图时屏蔽ViewController

时间:2017-07-14 13:58:47

标签: ios swift swift3

当我在this screenshot.中呈现一个视图时,我试图屏蔽一个ViewController。我呈现我的视图的方式是将它锚定在屏幕之外,如下所示:

addContactTopAnchor = addContact.bottomAnchor.constraint(equalTo: 
view.topAnchor)
addContactTopAnchor.isActive = true
addContact.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
addContact.heightAnchor.constraint(equalToConstant: 182).isActive = true

当我按下按钮时,我将它移动到显示屏上,如下所示:

 func addTapped(){
        self.addContactTopAnchor.isActive = false
        self.addContactTopAnchor =  addContact.bottomAnchor.constraint(equalTo: view.topAnchor, constant: 182)
        self.addContactTopAnchor.isActive = true
        UIView.animate(withDuration: 0.3, animations: { () -> Void in
            self.view.layoutIfNeeded()
        })
    }

我希望屏蔽除视图之外的所有内容,就像我在屏幕截图中显示一样。

1 个答案:

答案 0 :(得分:1)

我创建了一个视图

let mask: UIView = {
   let view = UIView()
    view.backgroundColor = .black
    view.alpha = 0
    return view
}()

然后在我的按钮按下,我设置了alpha并将我的视图带到了前面

func addTapped() {
    self.addContactTopAnchor.isActive = false
    self.addContactTopAnchor =  addContact.topAnchor.constraint(equalTo: view.bottomAnchor, constant: -182)
    self.addContactTopAnchor.isActive = true
    UIView.animate(withDuration: 0.3, animations: { () -> Void in
        self.view.layoutIfNeeded()
        self.mask.alpha = 0.5
        self.navigationController?.navigationBar.alpha = 0.5
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.view.bringSubview(toFront: self.addContact)
    })
}

编辑:我也添加了这个以掩盖导航栏:

      self.navigationController?.navigationBar.alpha = 0.5
      self.navigationController?.navigationBar.shadowImage = UIImage()

感谢@desdenova的帮助