Friday, March 25, 2016

Even more easily use a Navigation Bar without a corresponding Navigation Controller!

Well, so I wrote up this set of steps to add a navigation bar independent of a navigation controller:
Easily use a Navigation Bar without a corresponding Navigation Controller

Only to find that there's an even EASIER way!

Posted by joerick on Stack Overflow, you can do this in 3 steps - completely within the storyboard:

1) Add your navigation bar.

2) Add your constraints:
  • Navigation Bar.Top = Top Layout Guide.Bottom
  • Navigation Bar.Leading = Superview.Leading
  • Navigation Bar.Trailing = Superview.Trailing

3) In the Identity Inspector for the bar, add a user defined attribute:
  • Key Path: barPosition
  • Type: Number
  • Value: 3

That's it!

Many thanks to joerick! If this helps you, give 'em a vote up:
ios - Is there a way to change the height of a UINavigationBar in Storyboard without using a UINavigationController? - Stack Overflow


Again, here are some GIFs to show you the details:

1) Add your navigation bar. Just center it and click it to the guide. Don't worry about the weird space at the top.





2) Set the constraints for the navigation bar in the storyboard. All should have a constant of zero and a multiplier of one. Note: Ctrl-click to connect the navigation bar with the view in the outline; shift-click to select multiple items in the list:
    - Navigation Bar.Top = Top Layout Guide.Bottom
    - Navigation Bar.Leading = Superview.Leading
    - Navigation Bar.Trailing = Superview.Trailing




3) In the Identity Inspector for the bar, add a user defined attribute for barPosition, give it a type of Number, and give it a value of 3.






4) As you can see, you still get the same result - a normal, autoresizing navigation bar!







No comments:

Post a Comment