pprevalon pprevalon - 1 month ago 11
Swift Question

How can I make my contents go below the navigation controller without adjusting the content y position programmatically

How can I make my contents go below the navigation controller without adjusting the content y position programmatically . The reason I ask that is because when I adjust the content using y position it look fine but when the device is rotated I get a white gap because the navigation bar height change.

This is an image of adjust view  y position portrait
enter image description here

My code

import UIKit

class ProfileViewController: UINavigationController {


let topProfileView = UIView()

override func viewDidLoad() {

view.backgroundColor = .white

topProfileView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(topProfileView)
topProfileView.backgroundColor = .red
topProfileView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
topProfileView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
topProfileView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
topProfileView.heightAnchor.constraint(equalToConstant: 100).isActive = true
topProfileView.topAnchor.constraint(equalTo: view.topAnchor, constant:44).isActive = true

}

Answer

I present the view controller as a UINavigationController this will display the the nav bar

Comments