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

前端 未结 6 884
轻奢々
轻奢々 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:41
    override func viewDidAppear(_ animated: Bool) {
    let label = UILabel(frame: CGRect(x: 0, y: (navigationController?.navigationBar.frame.height)! + 20, width: UIScreen.main.bounds.width, height: 40))
    label.translatesAutoresizingMaskIntoConstraints = true
    label.text = "Hello"
    label.backgroundColor = UIColor.red
    self.view.addSubview(label)
    
    }
    

    0 讨论(0)
  • 2021-02-04 11:42

    Frame of navigationBar is (0 20; 375 44), you can set label y position to 64.

    0 讨论(0)
  • 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!!

    0 讨论(0)
  • 2021-02-04 11:52

    Replace this

    self.label = UILabel(frame: CGRect(x: 0, y: navigationBar.frame.height, width: 300, height: 100))
    

    with

    self.label = UILabel(frame: CGRect(x: 0, y: 64, width: 300, height: 100))
    

    naviagtion bar & status bar together has a height of 64. Make it the y position

    0 讨论(0)
  • 2021-02-04 11:54

    Swift

    Try this code with NSLayoutConstraint. newView will appear right under NavigationBar

            self.edgesForExtendedLayout = []//Optional our as per your view ladder
    
            let newView = UIView()
            newView.backgroundColor = .red
            self.view.addSubview(newView)
            newView.translatesAutoresizingMaskIntoConstraints = false
            if #available(iOS 11.0, *) {
                let guide = self.view.safeAreaLayoutGuide
                newView.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
                newView.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
                newView.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
                newView.heightAnchor.constraint(equalToConstant: 50).isActive = true
            } else {
                NSLayoutConstraint(item: newView,
                                   attribute: .top,
                                   relatedBy: .equal,
                                   toItem: view, attribute: .top,
                                   multiplier: 1.0, constant: 0).isActive = true
                NSLayoutConstraint(item: newView,
                                   attribute: .leading,
                                   relatedBy: .equal, toItem: view,
                                   attribute: .leading,
                                   multiplier: 1.0,
                                   constant: 0).isActive = true
                NSLayoutConstraint(item: newView, attribute: .trailing,
                                   relatedBy: .equal,
                                   toItem: view,
                                   attribute: .trailing,
                                   multiplier: 1.0,
                                   constant: 0).isActive = true
    
                    newView.heightAnchor.constraint(equalToConstant: 50).isActive = true
            }
        
    
    0 讨论(0)
  • 2021-02-04 11:54

    You're not calculating the height of the status bar with the navigation bar. In total they are 64, 44 nav bar and 20 for the status bar

    0 讨论(0)
提交回复
热议问题