How do I programmatically add a view right under the navigation bar?

前端 未结 6 893
轻奢々
轻奢々 2021-02-04 11:25

I\'m trying to add a view to a UINavigationController with its top aligned with the navigation bar\'s bottom.

I tried using constraints by adding the follo

6条回答
  •  天涯浪人
    2021-02-04 11:48

    Height of status bar is 20.You should consider status bar also while assigning y of your label. Your viewDidAppear should be

    override func viewDidAppear(_ animated: Bool) {
    
        self.label = UILabel(frame: CGRect(x: 0, y: navigationBar.frame.height+20, width: navigationBar.frame.width, height: 100))
        self.label?.translatesAutoresizingMaskIntoConstraints = false
        self.label?.backgroundColor = UIColor.red
        self.label?.text = "label text"
        self.view.addSubview(self.label!)
     }
    

    Hope it helps. Happy Coding!!

提交回复
热议问题