Search code examples
swiftautolayout

Autolayout with Navigation Item


I'm trying to pin my UIImageView to the top via Autolayout and don't quite understand how to pin it to the Navigation Item itself.

It turns out to the view itself

How to write this line correctly?

imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10) = true

Solution

  • The navigation bar is automatically added as a part of the safe area. You need to pin the top of your view to the safeAreaLayoutGuide like this:

    imageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10) = true
    

    Same goes for the tab bar.