iOS Right navigation button does not display in UINavigationBar - ios

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.

Related

Hide bar button items swift

How can I hide my left bar button item?
In my storyboard I dragged a Navigation Bar onto my View Controller, then a Bar Button Item. Under certain conditions I want to hide the Bar Button Item.
None of this works:
override func viewDidLoad() {
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.leftBarButtonItems = []
self.navigationItem.setLeftBarButtonItems([], animated: true)
}
I dragged a Navigation Bar onto my View Controller
Well, don't! There is a big difference between a navigation controller interface, where you set the navigationItem, and a loosey-goosey navigation bar just sitting there in the interface, which is what you have.
Embed your view controller in a UINavigationController and do things the right way. Then setting your navigationItem and its properties will work as expected.
You can't access to self.navigationItem.leftBarButtonItem because you manually drag navigationBar from storyboard. I would suggest to do the following instead:
add an IBOutlet of BarButtonItem (eg: barButton) that you created in storyboard
barButton.title = ""
barButton.isEnable = false
This will hide your BarButtonItem, and you can simply show it back later.

UIView overlapping Navigation Bar

In a viewController I programmatically create a UIView that has the same height of the screen. The problem is that navigation bar is still visible and clickable, but I want it to go under the new view. How can I do that?
EDIT: this is a screenshot of what I have now
Not sure if this is what you actually want, since hiding it is a quite acceptable thing to do. However you can hide the rightButtonItem and disable the left one:
self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.backBarButtonItem.enabled = NO;
And to get back your right bar button, if you need it again somewhere:
self.navigationItem.rightBarButtonItem = self.*whatever*ButtonItem;
See if that works. I'm away from my Mac at the moment, so can't check it myself.
Right now you have taken navigation controller as a root view
controller (Maybe),In this case navigation controller overlaps the
UIVewController's view that's why it comes on the view so you need to
hide the Navigation controller.
What about making it hidden?
self.navigationController.navigationBarHidden = YES;

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;
}

Can't do ANYTHING with navigation bar

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.

creating button for popover view anchor at run time

This may not be possible, but I'm hoping someone will have an idea how to do it.
I have an app I'm porting from iPhone only to Universal. On the iPhone, I'm using a Tabbed application. I use three tabs for the normal data to be displayed. I have a forth tab that's only displayed if certain conditions are met. To add the tab, I do:
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
UITabBarController *tabController = (UITabBarController *) self.rootViewController;
NSMutableArray* newArray = [NSMutableArray arrayWithArray: tabController.viewControllers];
[newArray addObject: [theStoryboard instantiateViewControllerWithIdentifier: #"AdditionalView-Phone"]];
[tabController setViewControllers:newArray animated:YES];
}
For the iPad, I have enough space on the initial view to display everything from the main three tabs in the iPhone UI. So all I need is one additional (small) view for the "Additional" data. I wanted to do it using a popOver view, so I set up the initial view with a Nav bar and popover button as in the Utility App template. But now I'm stuck. I can't figure out how to create that popover button at run time and make it do the segue to the popOver view properly. I can add the button like this:
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] initWithTitle: #"Modem" style: UIBarButtonItemStylePlain target: self action: #selector(togglePopover:)];
self.navBar.topItem.rightBarButtonItem = flipButton;
but I get an exception: 'NSInternalInconsistencyException', reason: 'UIStoryboardPopoverSegue must be presented from a bar button item or a view.' I'm pretty sure this is because I don't have an anchor set for the popOver segue. The button doesn't exist in the storyboard, so I can't set it there. And I can't seem to find an API to set it at run time.
I also tried creating the button in IB, but not in the view hierarchy, and then just setting the rightBarButtonItem property to my existing button. That also works, but I still can't set that button as the anchor for the popover view. I can set the Navigation Bar as the anchor, but that makes it anchor to the title in the nav bar, which looks silly.
Any ideas?
I had the same problem and solved it by creating a UIBarButtonItem in the Storyboard for the view controller but not part of the view hierarchy.
In IB, Drag a bar button item to the dark bar below the view controller view, drop it next to the "First Responder" and "View Controller" icons. Create a (strong) IBOutlet for it. Then create a popover segue from it to the destination view controller by dragging from the bar button item to the destination. It seems like this is the only way to set it as the anchor. Choosing it as the anchor for an existing segue does not work (looks like an IB bug).
In viewDidLoad you can assign this bar button item to the navigationItem (or where ever you like) and the segue works as expected.
I was curious about this too so I made a quick test project. You're right, there doesn't seem to be a way to configure the popover segue at runtime or add an anchor point to a button that's not in the view hierarchy using Interface Builder.
My solution was to set everything up in IB with the UIBarButtonItem visible and connected to an IBOutlet property, then remove it from the navigation bar in -viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = nil;
}
Then I simply add it back or remove it by tapping another button:
- (IBAction)toggleBarButtonItem:(id)sender
{
UIBarButtonItem *item = (self.navigationItem.rightBarButtonItem == nil) ? self.popoverBarButtonItem : nil;
[self.navigationItem setRightBarButtonItem:item animated:YES];
}
You could conditionally keep or remove the button in -viewDidLoad the same way. The segue remains anchored to the UIBarButtonItem.
I'm gonna try making a dummy view that's the size and shape of the views I want to present the popover from, wire that to the segue popover target, and then move the view to the right position in prepareForSegue:sender:
I'm not sure this is exactly what you want, but this is what I would do. Create a button and set it up with some target/action. Call that target/action method
presentPopover:(UIButton *)sender;
Then in the presentPopover method, say
UIViewController *customAdditionalViewController = [[MySpecialVC alloc] init];
//Configure the customAdditionalViewController
//Present the customAdditionalViewController in a Popover
UIPopoverController *popover = [[UIPopoverController alloc] initWithViewController:customAdditionalViewController];
//Set popover configurations
[popover presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:/*whatever you want*/ animated:YES];
That is how I would handle your use case.

Resources