How to add UIView over navigation bar?

前端 未结 5 440
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 15:38

I need overlay UINavigationBar with UIView like here

\"http://screencast.com/t/ZKXNFcAzVu

5条回答
  •  野的像风
    2021-02-02 15:43

    Here's how implemented mine, hope this helps..

      override func viewWillAppear(_ animated: Bool) {
        let testView = UIView(frame: .zero)
        testView.backgroundColor = .black
        testView.layer.cornerRadius = 10
        testView.translatesAutoresizingMaskIntoConstraints = false
        self.navigationController?.navigationBar.addSubview(testView)
        NSLayoutConstraint.activate([
          testView.widthAnchor
            .constraint(equalToConstant: 50),
          testView.heightAnchor
            .constraint(equalToConstant: 70),
          testView.topAnchor
            .constraint(equalTo: (self.navigationController?.navigationBar.topAnchor)!),
          testView.trailingAnchor
            .constraint(equalTo: (self.navigationController?.navigationBar.trailingAnchor)!, constant: -20)
          ])
        self.customView = testView
      }
      
      // i dont need to display the overlay in every page
      // so i remove it everytime i navigate to a new page
      override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.customView.removeFromSuperview()
      }
    

提交回复
热议问题