ios 如何通过编程方式在导航栏的正下方添加视图?

5rgfhyps  于 2023-03-14  发布在  iOS
关注(0)|答案(7)|浏览(151)

我正在尝试向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])
}

结果是:

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

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

}

结果是这样的

在这两种情况下,我的标签都覆盖了导航栏的一部分。如何解决这个问题?

vcudknz3

vcudknz31#

斯威夫特

尝试使用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
        }
goucqfw6

goucqfw62#

状态栏的高度为20。在分配标签的y时也应考虑状态栏。您的viewDidAppear应为

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

raogr8fs3#

你没有计算导航栏和状态栏的高度,总共是64,导航栏44,状态栏20

n3h0vuf2

n3h0vuf24#

替换此

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位置

9gm1akwq

9gm1akwq5#

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)

}

edqdpe6u

edqdpe6u6#

如果任何人仍然有同样的问题,或者想要有同样的东西,但在navigationBar大标题,你可以添加下面的代码到你的子视图约束:
iOS 11.0或以上版本:

yourSubView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true

iOS 11.0或更低版本:

yourSubView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
35g0bw71

35g0bw717#

导航栏的框架为(0 20;375 44),则可以将标签y位置设置为64。

相关问题