Add a UIView above all, even the navigation bar

前端 未结 17 1679
悲&欢浪女
悲&欢浪女 2020-11-29 15:36

I want to display, above any other views, even the navigation bar, a kind of \"pop-up\" view that looks like this:

  • full screen black background with a 0.5 alph
17条回答
  •  死守一世寂寞
    2020-11-29 16:02

    @Nam's answer works great if you just want to display your custom view but if your custom view needs user interaction you need to disable interaction for the navigationBar.

    self.navigationController.navigationBar.layer.zPosition = -1
    self.navigationController.navigationBar.isUserInteractionEnabled = false
    

    Like said in Nam's answer don't forget to reverse these changes:

    self.navigationController.navigationBar.layer.zPosition = 0
    self.navigationController.navigationBar.isUserInteractionEnabled = true
    


    You can do this in a better way with an extension:

    extension UINavigationBar {
        func toggle() {
            if self.layer.zPosition == -1 {
                self.layer.zPosition = 0
                self.isUserInteractionEnabled = true
            } else {
                self.layer.zPosition = -1
                self.isUserInteractionEnabled = false
            }
        }
    }
    

    And simply use it like this:

    self.navigationController.navigationBar.toggle()
    

提交回复
热议问题