首页 文章

如何以编程方式在导航栏下添加视图?

提问于
浏览
2

我正在尝试将视图添加到 UINavigationController ,其顶部与导航栏的底部对齐 .

我尝试使用约束,将以下内容添加到我的 UINavigationController 子类中:

override func viewDidAppear(_ animated: Bool) {
           self.label = UILabel()
    self.label?.translatesAutoresizingMaskIntoConstraints = false
    self.label?.backgroundColor = UIColor.red
    self.label?.text = "label text"
    self.view.addSubview(self.label!)
    let horConstraint = NSLayoutConstraint(item: label!, attribute: .top, relatedBy: .equal,
                                           toItem: topLayoutGuide, attribute: .bottom,
                                           multiplier: 1.0, constant: 0.0)
    let widthConstr = NSLayoutConstraint(item: label!, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)

    let heightConstr = NSLayoutConstraint(item: label!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)
    view.addConstraints([horConstraint, widthConstr, heightConstr])
}

这是结果:

enter image description here

我尝试设置子视图的框架:

override func viewDidAppear(_ animated: Bool) {

    self.label = UILabel(frame: CGRect(x: 0, y: navigationBar.frame.height, width: 300, height: 100))
    self.label?.translatesAutoresizingMaskIntoConstraints = false
    self.label?.backgroundColor = UIColor.red
    self.label?.text = "label text"
    self.view.addSubview(self.label!)

}

这出来了:

enter image description here

在这两种情况下,我的标签都覆盖导航栏的一部分 . 我该怎么解决这个问题?

6 回答

  • 2

    状态栏的高度为20.您在分配 y 标签时也应考虑状态栏 . 你的_683640应该是

    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!)
     }
    

    希望能帮助到你 . 快乐编码!!

  • 1

    斯威夫特4 .

    enter image description here

    使用 NSLayoutConstraint 尝试此代码 . newView将出现在 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
            }
    
  • 1

    navigationBar的框架为(0 20; 375 44),您可以将标签y位置设置为64 .

  • 0

    您没有使用导航栏计算状态栏的高度 . 它们共有64个,44个导航条和20个状态栏

  • 1

    替换它

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

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

    导航栏和状态栏一起高度为64.使其成为y位置

  • 6
    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)
    
    }
    

    enter image description here

相关问题