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 following to my UINavigationController subclass:
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])
}
With this being the result:
And I tried by setting the frame of my subview:
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!)
}
And this came out:
In both cases my label covers part of the navigation bar. How to I fix this?


