I have a project which uses a tab bar. All the tabs in the tab bar have the same main navigation controller. On one of the views I want to have custom buttons which the user can use.
I am having problems adding these buttons to the storyboard and having them shown:
- Programatically adding them
My project is written in Objective-C so I tried adding them programatically to my viewController:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(myFriendsView)];
I also tried adding them using the following:
self.navigationController.navigationItem.rightBarButton = ...
As the initial method didn't work. I didn't know if I needed a pointer to the tab bar navigation controller to add a button. Neither of these approaches worked.
- Adding the buttons in the storyboard
I added a navigation item to my viewController in the storyboard and then dragged my buttons into it. You can see a screenshot below:
The problem with this technique was that the buttons didn't appear when I loaded the code. Once again I thought that this might be to do with the UITabBar navigation controller overwriting the storyboard.
- Add a navigation controller to the storyboard to load the viewController
I next tried adding a UINavigationController and then loading my viewController as the root view. The root view still containing the navigation item and the buttons.
This was the first method that seemed to have any effect on the functionality I was after. The weird thing was that the navigation item was added below the navigation bar:
As you can see it has added the navigation items below the navigation bar instead of inside it. This functionality works as hoped except for the buttons being in the wrong place.
- Other things I have tried:
-Dragging the UIBarbuttonItems into the navigation controller nav bar in the storyboard
Conclusion: To me this seems like it should be an easy thing to achieve with storyboards. I can't understand what is causing this code to not work correctly and why Apple would enable functionality that adds the navigation items under a nav bar without me adding a navigation bar into the same view in the storyboard.
If anyone can give me advice on using storyboards and how to add navigation buttons to a view controller when using a tab bar that would be very appreciated.