I have a side menu that slides out to display a table view and from there I have segues that use the reveal view controller. The segue has to connect directly to the view controller; I can't use a navigation controller.
How do I add a navigation bar with a bar button item without a navigation controller?
be very careful about adding in "viewWillAppear", as this method can be called more times, (for example is a modal appears....) so use a lazy approach:
1) declare a var:
2) test if already set:
3) be sure remove exiting controller, for example on didDisappear...
note.. is not correct to specify size.. if iOS rotates, it does not work fine..
While there are several smart ways to answer your question. I just solved it programmatically and wrote the following code in my
viewWillAppear
(note -viewDidLoad
is also okay, but not suggested) -So, you have a white navigation bar with blue bar button items without a Navigation controller. Again, there are other ways to implement it in your case. Hope, this was helpful.
Output -
Update -
To add images -
There is a way to use the
NavigationItem
in interface builder for this.First add a
NavigationItem
to yourViewController
in the interface builder, like you would with aNavigationController
. Make sure to make theNavigationBar
is visible by selecting something other thanInferred
andNone
under simulated metrics.Second, in
viewDidLoad
, just add the following lines:As for
frame
,width
will be the same as yourViewController
andheight
will be either44.0
or64.0
depending if thestatus bar
is visible or not.And if you wish to support different orientations use
NSLayoutConstraints
:Swift 4 Version
be very careful about adding in "viewWillAppear", as this method can be called more times, (for example is a modal appears....) so use a lazy approach:
1) declare a var:
2) test if already set:
3) be sure remove exiting controller, for example on didDisappear...