Can't do ANYTHING with navigation bar - ios

I have this structure:
When I want to add a ´UINavigationItem´ from the storyboard the navigation bar is "disabled", so I tried to add the right button programatically:
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(showPickerView:)];
self.navigationItem.rightBarButtonItem = button;
But nothing happens. I renamed the navigation bar title from storyboard, but when I run the app the title is not set. I really don't know what is the source of the problem. There is just the back button that is appearing.
Thank you for your help.

Each item in the tab bar controller should have a navigation controller as the root controller (well, you don't need all of them to have a nav controller if you don't need them). What you currently have is a tab bar controller in the navigation controller (unless it's modal) so the view controllers contained in the tab bar controller can't see out to the navigation controller.

As other answers have pointed out, the hierarchy of your UINavigationViewController is incorrect. The UITabBarViewController will always be the parent of your UINavigationViewControllers.
For each UIViewController that is a "Tab", you need your UINavigationViewController to be "in front" of each "tab" UIViewConrolller that will have segues.
So you do not need a UINavigationViewController for every "tab" UIViewController, as someone else pointed out on this thread, only for "tabs" that will segue to other Views that you want to track navigation with a UINavigationViewController
The hierarchy can be seen in #App Dev Guy's answer here
In Swift 2, Xcode 7 has a very handy feature for adding a UINavigationController:
Select the UIViewController that is being used as a "tab" for the UITabBarNavigationController
On the top Xcode menu, select "Editor" ->
"Embed In" ->
"Navigation Controller"

I tried to reproduce your problem.
I started a new 'Single View' project.
Selected the iPhone storyboard
Selected the view controller
Menu -> Editor -> Embed in -> Navigation Controller.
Open the object browser
type 'Bar Button Item' in the search bar
draw the bar button item over the navigation bar.
Done. See if you can do the same.

Try it like this. It's a little different than how you are doing it and it works for me:
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithTitle:#"title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(showPickerView:)];
self.navigationItem.rightBarButtonItem = button;
If you want to do this in the Storyboard, you don't drag the "Navigation Item", but the "Bar Button Item" instead.

Related

ENSideSwiftMenu: how to use same Navigation Bar for all View Controllers

I am using Evgeny Nazarov's Swift Side Menu for my iOS app and was wondering if anyone has used this same library and able to have the same Navigation Bar for every View Controller that is called from the menu.
I put a BarButtonItem that toggles the sliding menu on my Root View Controller. My problem is that only my Root View Controller shows this Toggle Button. But I would like to have that same Navigation Bar with the Toggle button on every View Controller that is accessible when clicking an option on the sliding menu.
Has anyone had an experience achieving this? Thanks!
UPDATE 1
This is what happens when I added a NavigationBar and BarButtonItem to the other ViewControllers that aren't the RootViewController. The new Navigation Bar ends up under the existing Navigation Bar from the Navigation Controller.
The screen on the left is the Root View Controller and the one on the right is the View Controller when I select the second item on the menu.
UPDATE 2: SOLVED
override func viewDidLoad() {
super.viewDidLoad()
let menuItem = UIBarButtonItem(image: UIImage(named: "icon_menu"), style: .Plain, target: self, action: "menuBarButtonItemClicked")
menuItem.tintColor = UIColor.whiteColor()
self.navigationItem.leftBarButtonItem = menuItem
}
func menuBarButtonItemClicked() {
toggleSideMenuView()
}
You need to put a UIBarButtonItem on the navigation bar in every view controller, not in the root view controller.
I too have used the same design for my app and I solved it this way:
View every sect "Show the Attribute Inspector -> Top bar" the voice "Translucent Navigation Bar", then drag the "Navigation Item" and then "Bar Button Item" in the position you want. Of course every button should have its code or references to the ViewController.
Let me know if you have solved.

How to add Navigation bar to view controller with tab bar item

I have an app that uses bottom tabs aswell as a side menu, to have the button that initiates the side menu i use the typical three line menu button, to put that there I have a Navigation Bar. With the bar in place there is no way I can get the bar to be on top of the screen. I built it with interface builder, and heres a screenshot. The question is how do i have the navigation bar alone without the other grey bar above it?
The issue you're encountering is due to the fact that you're manually creating a navigation bar for your view controller, instead of using the bar that you get for free by embedding the view controller in a tab bar controller, hence the reason you see two bars. The other answer suggesting hiding the auto-generated navigation bar is not the correct solution. Instead, you should place your menu button and view title in the auto-generated bar instead of manually creating your own (you almost never want to do that, in-fact).
So what you should do instead is set the title property of your view controller to be "News", and the leftBarButtonItem property of the view controller to be your hamburger menu button (an instance of UIBarButtonItem initialized with an image for the icon).
For example (inside your view controller's viewDidLoad method or wherever appropriate):
self.title = #"News";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"menuIcon"] style:UIBarButtonItemStylePlain target:self action:#selector(showSideMenu)];
If you want to remove the topmost navigation bar you need use self.navigationController.navigationBarHidden = YES; for view controllers that used for tabs in UITabBarController:
// StoriesViewController.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}

iOS Navigation Bar: change left bar button from menu to back

I have an app that uses a side menu and has a few main screens that can be accessed from the menu and others that can only be accessed from these screens.
What I want is to have a menu button on the navigation bar that opens the menu and can only be visible on the main screens. On the other screens I want to have a back button, instead of the menu button.
I've already put the menu button like this:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"IconMenu"] style:UIBarButtonItemStylePlain target:self.revealViewController action:#selector(revealToggle:)];
But I can't figure out how to change it with the back button when I need it.
Assuming than by "main screens" you mean root (first) view controllers in the navigation view controllers corresponding to the selected side menu items, this might be a solution for your problem: you can create a superclass for all your view controllers, say MyBaseViewController and rewrite viewWillAppear: method, that will determine whether it should have a default back button or a "revealSideMenu" button, based on whether it's a "main screen" or not.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self == [self.navigationController.viewControllers firstObject]) {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"IconMenu"] style:UIBarButtonItemStylePlain target:self.revealViewController action:#selector(revealToggle:)];
}
}
you can do that in 2 different ways:
the first is in the viewDidAppear check if the back button is already present (when you push a VC the back button automatically added but is avalaible programmatically only after the viewDidAppear) and then decide to add or not the menu button,
the second is add a parameter to your VC init method like isRoot or hasMenu or whatever you like to name it, and using that flag decide to add the menu or the back button
if you chose to add your own back button you have to call this method in your back selector
[self.navigationController popViewControllerAnimated:YES];
The easiest way, IMO, is just to click on the title bar of the first ViewController and in the Attribute Inspector (⌥+⌘+4) change the Navigation Item info the way you want: Title -> what will show up in the back button* or if you want it to say something other than the Title of the first ViewController or the word "Back" you can just put it in the Back Button field.
*If the Title of the second ViewController is too long for it to fit it will be replaced with the word "Back" (and if the word "Back" doesn't fit it will only have the arrow sign).

UINavigationBar doesn't appear

I can't understand why Navigation Bar doesn't appear.
For example, I tried to add Navigation Bar like this:
1) File -> new project -> single view application -> storyboard -> viewcontroller -> attributes inspector -> Top Bar -> Navigation Bar.
It appear at storyboard, but doesn't appear when app run on simulator
Also I can't understand why it doesn't appear as a subview
In viewDidLoad:
UINavigationBar *navBar = [[UINavigationBar alloc] init];
[self.view addSubview:navBar];
That's not how you add a navigation bar to a view in Storyboard.
The normal way to do this, is to select your View Controller in your story board, then choose "Editor" in your menu, then "Embed in" => "Navigation Controller".
You should see a navigation controller appears with an arrow connecting to your original view controller.
Then, choose your newly appeared navigation controller, check the "Is Initial View Controller" checkbox in the inspector, and viola, that should give you what you need.
And you don't customise your navigation bar though UINavigationBar. Instead, you customise what appears in your navigation bar by changing your view controller's navigationItem.
In your view controller's viewDidLoad:, try the following:
self.navigationItem.title = #"My Title";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCamera target: self action: #selector( whatEverMethod: )];
And you should see a right bar button and a title being set in your view controller.
--
What the "attributes inspector" => "top bar" does is to show you what the view will look like if it is embedded in a navigation controller. it does not actually add the navigation bar for you.
If you look closely, you will see that the "top bar" option is in a section called "simulated metrics. (They are just simulated, not really added to your view)

iOS Right navigation button does not display in UINavigationBar

I have following view controllers hierarchy:
View Controller VC1
Tab Bar Controller TBC1 - configured in storyboard to lead to a Table View Controller TVC1 and a Map View Controller MVC1
Table View Controller TVC1
Table View Controller TVC2
In VC1, I do this:
[self.navigationController pushViewController:TBC1 animated:YES];
This rightly brings up tab bar controller, with TVC1 in focus.
TVC1 shows back button in its navigation bar (programmatically created from VC1 code), which will get me to VC1, which is expected.
However, from TVC1 onwards, I need one more navigation to TVC2. I am trying to add right button to the TVC1 navigation bar for this, but it doesn't show up.
Here is the code I use in TVC1 (rightButton is UIButton type property of TVC1):
self.rightButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem: UIBarButtonSystemItemAdd
target: self
action: #selector(MySelector:)];
self.rightButton.style = UIBarButtonItemStyleBordered;
self.rightButton.title = #"";
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: self.rightButton, nil];
(specified blank title and style just to ensure if that's the issue which is causing this, I don't actually need those values)
MySelector is declared in TVC1.h as:
- (void) MySelector:(id)sender;
And it is properly implemented, too.
But rightButton above does not display in TVC1 navigation bar.
What am I missing?
I suspect its with TBC1 (tab bar) that comes between VC1 and TVC1, and somehow it resets navigation properties.
But then I argue that I see navigation bar on TVC1, and a left button leading to VC1.
I checked that in TBC1, self.navigationItem.rightBarButtonItems has 1 object inside which is definitely the rightButton I am adding.
Where am I wrong?
Note: Above is found in all of iOS 5.0, 5.1 and 6.0 simulators.
It seems to me that your are missing UINavigationController between TVC1 and TVC2 in your storyboard. If you are using storyboards then you can create navigation item Add button type on the navigation controller itself and have a PUSH segue to TVC2. See this diagram if that makes sense. If this doesn't solve your problem then please upload example code and I will have a look.
[EDIT]
I had reproduced your issue by creating your view controllers structure in storyboard.
If you notice here TVC1 doesn't have it's UINavigationController but it is inheriting it from VC1. Solution to your problem is rather than adding rightButton onto self add it to self.parentViewController and you will see rightButton in TVC1. But mind you it will also appear in MVC1 as it is belong to TBC1's parent. You can hide right bar button in MVC1's viewWillAppear if you don't want it there. Following is the code.
self.rightButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem: UIBarButtonSystemItemAdd
target: self
action: #selector(MySelector:)];
self.rightButton.style = UIBarButtonItemStyleBordered;
self.parentViewController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: self.rightButton,nil];
If you want to Add right button into the TVC1's navigation controller then you need Embed TVC1 into UINavigationController. To do this, select TVC1 screen in the storyboard -> Editor -> Embed In->Navigation Controller. When you do this your code will also work and will show you right button but you will have two navigation controllers(see image below) in it because of your structure of storyboard. You will need to hide Parent's navigation controller in to the TVC1's view did load and have left button to Pop to Parentview Controller. You do the same in MVC1.
Hope this helps! Happy coding :)
Had the same problem recently in Swift and found that embedding the child view in a navigationController was still the correct way to be able to access the rightBarButtonItems.

Resources