Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pin a UIView's top to the bottom of the navigation bar?

I'm trying to position my UIView to be 20pt under the navigation bar, but when I set it relative to the view on the view controller it's still under the navigation bar at 20pt, and I don't want to hardcode it.

Is it possible to position it away from the navigation bar?

like image 716
Doug Smith Avatar asked Nov 10 '14 06:11

Doug Smith


People also ask

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.

How do I hide the bottom navigation bar in Swift?

Way 1: Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.


2 Answers

To do this programmatically use the topLayoutGuide of your view controller:

override func viewDidLoad() {
    ...
    myView.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor).isActive=true
    ...
}
like image 78
andrewz Avatar answered Oct 13 '22 22:10

andrewz


Try adding contraint with TopLayoutGuide,

The main difference between TopLayoutGuide and self.view is, top layout guide starts with bottom of status bar + bottom of navigation bar(if exist), but self.view always start from (0,0) coordinate in iOS 7 for translucent navigation bar.

So in your case you should try pinning from top layout guide.

Top Layout Guideself.view

like image 36
Mohd Iftekhar Qurashi Avatar answered Oct 13 '22 21:10

Mohd Iftekhar Qurashi