Replacing UITabBar with UIToolbar programmatically - objective-c

I'm trying to write a tabbed application where each tab is a Navigation Controller. This tab bar appears at every view in the navigation controller as its being inferred on each view.
I would like to replace this tab bar on a detail view with a tool bar with a couple of buttons on it.
I've tried the following code in that detail view's viewDidLoad: method
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *accept = [[UIBarButtonItem alloc] initWithTitle:#"Accept"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
UIBarButtonItem *decline = [[UIBarButtonItem alloc] initWithTitle:#"Decline"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
NSArray *items = [NSArray arrayWithObjects:accept, decline, nil];
[self.navigationController.toolbar setItems:items animated:YES];
// code suggested
[self.view addSubview:self.navigationController.toolbar];
It still doesn't show up. Though hides the tab bar now for adding the following line in the view that's presenting the detail view:-
theDetailTableViewController.hidesBottomBarWhenPushed = YES;
Have I missed something?

I usually put toolbarHidden = YES or NO, as applicable, in the viewWillAppear or viewDidAppear methods. I am not sure if that is why it is not working for you, but you need to address when you return to the presenting view anyway.
If you don't address it, the toolbar will still be visible when you go back.

Wherever you are pushing your detailViewController from, do this to hide the Tab Bar in the detail view:
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.hidesBottomBarWhenPushed = YES;
[self.navigationController detailViewController animated:YES];
and in your detail view, just add the ToolBar as a subview to the detailView.

Related

Storyboard: How to set a UIToolbar at the bottom of a UITableViewController controlled by a UINavigationViewController

I'm new to the Cocoa topic. I build with the Storyboard a small application which runs just fine.
I'm pushing UITableViewController to another one, controlled by the UINavigationViewController. Now I need a UIToolbar with a UIBarButtonItem fixed at the bottom of the screen.
Here can you see my Storyboard So I added via drag and drop a Toolbar and in the toolbar an button into the Period TableViewController, thus it is on the same hierarchic level like the UITableView.
Unfortunately neither the "PayOff" Button nor a bottom Toolbar does appear in the app.
Can you help me, what is wrong with my Storyboard?
In your ViewController try adding this
[self.navigationController setToolbarHidden:NO animated:YES];
and use this to add BarItems to it
[self setToolbarItems:#[item1, item2, item3] animated:YES];
I'm using this method to add a scan button to a UITableViewController:
UIBarButtonItem *leftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
...
...
UIBarButtonItem *scanItem = [[UIBarButtonItem alloc] initWithCustomView:scanButton];
[self setToolbarItems:#[leftSpace, scanItem, rightSpace] animated:YES];
You should be careful because the ToolBar visibility is set for the entire NavigationController you are using and you should show/hide it when needed.
Also the items on it need to be set on each controller (I have this issue, maybe there is a better way to do it)
Hope this helps.

NAvigationController Bar Buttons not showing

I have NavigationViewController class and in viewDidload methods
like this. but Bar Buttons not showing any clue?
MapViewController *p=[[MapViewController alloc]initWithNibName:self.nibName bundle:[NSBundle mainBundle]];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithTitle:#"Code"
style:UIBarButtonItemStylePlain
target:self
action:#selector(showDocco:)];
[[self navigationController] setNavigationBarHidden:NO animated:NO];
self.navigationItem.rightBarButtonItem = rightButton;
self.modalPresentationStyle=UIModalPresentationFormSheet;
[self pushViewController:p animated:YES];
Yiğit, if you want to show the bar button items in MapViewController then your code should be like as follows.
p.navigationItem.rightBarButtonItem = rightButton;
Lets say. you are in HomeViewContrller and you want to push MapViewController on button press.
Your code is setting your bar button item in your HomeViewController becuase you set there self.navigationItem.rightBarButtonItem = rightButton; in viewDidLoad method inside HomeViewController. it will access the navigation item of HomeViewController.
In short, each and every viewcontroller has its own navigation items. Either you should specify the barbutton item with particular viewcontroller reference as i showed you in my answer Or you can transfer your barbutton item code inside MapViewController viewDidLoad method.
Hope this helps
you have given nib name and do you have navigation controller in the nib?If you dont have navigation bar then add one toolbar and add the bar button item to navigaiton bar.

Set default rightBarButtonItem for UINavigationController

Sup guys,
I have a tabBarController with a navigationController on each tab.
I wanted to set de default right bar button of navigation bar so I wouldn't have to write the same code on 3 different view controllers.
I tried [[UINavigationBar appearance] setRightBarButtonItem:myButton];
but had no success, is says:
-[_UIAppearance setRightBarButtonItem:]: unrecognized selector sent to instance
Then I tried to create my own UINavigationController subclass so I could set the button like:
self.navigationItem.rightBarButtonItem = myButton but again no success, nothing seems to happen probably because I dont have a navigationItem at this time.
Anyone have another solution?
If you inherit from the UIViewController you can set the right button like this
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"foobar.png"] style:UIBarButtonItemStylePlain target:self action:#selector(myAction)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];

Change autoresizingMask on programmatically created navigation bar

So I have a view-based app in which I need one page with a tableview and I wanted a navigation bar above it to go back. I added this programmatically with:
-(IBAction)switchToTableView; {
tableView *table = [[tableView alloc] initWithNibName:#"tableView" bundle:nil];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController: table];
table.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[table release];
[self presentModalViewController: navControl animated:YES];
[navControl release];
}
Then in the viewDidLoad of the tableview class I implemented:
- (void)viewDidLoad
{
self.tableView.rowHeight = 70.f;
UIBarButtonItem *back = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStyleDone
target:self
action:#selector(switchToHome)];
self.navigationItem.leftBarButtonItem = back;
UIBarButtonItem *map = [[UIBarButtonItem alloc]
initWithTitle:#"Map"
style:UIBarButtonItemStyleDone
target:self
action:#selector(switchBacktoFindArt)];
self.navigationItem.rightBarButtonItem = map;
[map release];
[super viewDidLoad];
}
to add the needed buttons. Now, all of this worked great, but now that I'm going through my app and allowing for orientation changes, I'm having trouble with this one since I can't change the autoresizingMask in IB. The whole tableview page resizes nicely except the navigation bar becomes very skinny top-to-bottom wise. I'd like it to stay the same height. How do I go about setting the appropriate Masks or even accessing the navigation bar outside of IB?
I suppose you can play with things like this to disable height changes:
navControl.navigationBar.autoresizingMask ^= UIViewAutoresizingFlexibleHeight
But a UINavigationController might really get wonky if you play around with its internals like that. I'm surprised that the nav's bar isn't already resizing to an appropriately pleasing height, like it's supposed to do by default. (It feels like something else might be wrong elsewhere in your code, but I'd have to see more to find out).

UIToolbar in a popover

Is it possible to show toolbar items in a UIViewController inside a popover? I'm doing this in the viewDidLoad method of my view controller:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
[self setToolbarItems:[NSArray arrayWithObject:addButton]];
[addButton release];
Then I'm wrapping this view controller in a UINavigationController (which has a toolbar property, and according to the docs, I'm supposed to use the setToolbarItems method of UIViewController to add items to the toolbar), then presenting it in a popover.
I do not see the toolbar. Are toolbars unsupported when using a popover?
Thanks
Figured it out, apparently the toolbar is hidden by default so you have to do this:
[self.navigationController setToolbarHidden:NO animated:NO];
To make it appear.