How to transition UIToolbar with it's parent view - ios

How can I transition from one UIViewController (with bottom toolbar) to another one (without toolbar), so that while the animation is in progress, the toolbar moves away with the first view, meaning that the toolbar stays in it's initial position of the first view?
I've seen this behaviour in "Things" app.
Just to be clear, I am not looking for solutions such as hiding/showing the toolbar inside viewDidAppear.

So, this is a solution I wasn't very happy about, but at the moment it seems as the only one.
The point is to ignore the built in toolbar property of UINavigationController, create separate UIToolbar and place it inside your view controller.
// Create your bar items
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects: flexibleSpace, nil];
[flexibleSpace release], flexibleSpace = nil;
UIToolbar *customToolbar = [[UIToolbar alloc] init];
[customToolbar sizeToFit];
[customToolbar setFrame:CGRectMake(0.0, self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height - customToolbar.frame.size.height, customToolbar.frame.size.width, customToolbar.frame.size.height)];
[customToolbar setItems:toolbarItems];
[[self view] addSubview:customToolbar];
[customToolbar release], customToolbar = nil;
[toolbarItems release], toolbarItems = nil;
This way the toolbar will slide away with it's view, causing no animation issue such as "white rectangles" or late appearing toolbar when placed in viewDidAppear...

It sounds like you want:
- ( void )viewWillAppear: (BOOL)animated
{
NSArray *items = ... UIBarButtonItem array...;
[ super viewWillAppear: animated ];
[ self setToolbarItems: items animated: animated ];
....
}

Related

UInavigatonbar items

I add a uinavigationcontroller as a subview on a uiviewcontroller (rootviewcontroller of aap) and this view controller is a subview as a rootviewcontroller on self.window.
in Appdelegate I use:
self.window.rootViewController = self.rootVC;
and in viewdidload of my RootViewController I wrote:
[self.navVC.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
[self.navVC.navigationController.navigationBar setTranslucent:YES];
self.navVC.navigationController.navigationBarHidden = NO;
[app.rootVC.view addSubview:self.navVC.view];
self.navVC.delegate = self;
Here, navigationbar is displaying but I am unable to display uibarbutton items on navigation bar. Example code:
leftBtn = [[UIBarButtonItem alloc] initWithTitle:#"Left" style:UIBar
flxBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBa
rightBtn1 = [[UIBarButtonItem alloc] initWithTitle:#"Right" style:UIBar
rightBtn = [[UIBarButtonItem alloc] initWithTitle:#"Right" style:UIBar
[self.navVC.navigationItem setLeftBarButtonItem:leftBtn];
[self.navVC.navigationItem setRightBarButtonItems:#[rightbtn1,rightbtn,flxBtn]];
Can somebody help me out to resolve my problem. How can I change appearance of navigationbar and how the uibarbutton items can be displayed ?
You will simply need to change your
[self.navVC.navigationItem setRightBarButtonItems:#[rightbtn1,rightbtn,flxBtn]];
to
[self.navigationItem setRightBarButtonItems:#[rightbtn1,rightbtn,flxBtn]];
and
[self.navVC.navigationItem setLeftBarButtonItem:leftBtn];
to
[self.navigationItem setLeftBarButtonItem:leftBtn];`

Navigation Bar default view

In my app I have a tableViewController where I have added a UINavigationController and a BackButton programmatically:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self];
[self.view addSubview:navController.view];
self.title =#"A";
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:#"back" style:UIBarButtonItemStyleBordered target:self action:#selector(backPressed:)];
self.navigationItem.leftBarButtonItem = btw;
But the output is not the same as the default navigation bar and as the default back button.
This is what I mean with default navigation bar and default back button:
And this is how it looks with my custom code:
How could I get the same output with code?
Try to set up navigationBar frame with following code:
self.navigationController.navigationBar = CGRectMake(self.navigationController.navigationBar.frame.origin.x, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width, 64);
and use this method:
-(UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
return UIBarPositionTopAttached;
}
Don't forget to set up delegate of navigation bar
self.navigationController.navigationBar.delegate = self;

iOS 7 backBarButtonItem hidden

I've an issue with back bar button. It stay hidden no matter that i do like self.navigationItem.hidesBackButton
Here is my code to add back button:
//
- (void)viewDidLoad{
[.....];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"UI_BTN_BACK", nil) style:UIBarButtonItemStylePlain target:nil action:nil];
back.tintColor = [Templates getColor:#"color"];
[[self navigationItem] setBackBarButtonItem:back];
// Parent
[super viewDidLoad];
}
Button stay hidden BUT going back works if if touch on the place where it should be.
Of course it works on iOS6.
Another detail: back button seems to appear when i set UINavigationBar translucent to YES.
Thanks
[self.navigationItem setHidesBackButton:YES];
Check out this.
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
Use this code:
self.navigationItem.backBarButtonItem = nil;
OR
self.navigationItem.leftBarButtonItem = nil;

UINavigationBar UIBarButtonItem works on left but not on right

I have two buttons on my navbar. One on the left and one on the right. The button on the left works but the one on the right does not work. If I swap the buttons, recompile and load the issue persist with the right button regardless of which button is in the right spot of the bar.
Also while the left button works as soon as I try to use the right button the left button stops working too.
How do I make the right button work? Below is the code.
UINavigationBar *NavBar = [[UINavigationBar alloc] init];
[NavBar sizeToFit];
[self.view addSubview:NavBar];
UIBarButtonItem *cmdBack = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cmdBackClicked:)];
UIBarButtonItem *cmdAddDevice = [[UIBarButtonItem alloc] initWithTitle:#"Add Device"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cmdAddDeviceClicked:)];
UINavigationItem *NavBarItems = [[UINavigationItem alloc] initWithTitle:#"Settings"];
NavBarItems.leftBarButtonItem = cmdBack;
NavBarItems.rightBarButtonItem = cmdAddDevice;
NavBar.items = [NSArray arrayWithObjects:NavBarItems, nil];
The functions for the buttons are these
- (void) cmdBackClicked:(id)sender
{
[self.view removeFromSuperview];
}
- (void) cmdAddDeviceClicked:(id)sender
{
vcAddDevice *ViewAddDevice = [[vcAddDevice alloc] initWithNibName:#"vcAddDevice" bundle:nil];
[ViewAddDevice SetEditDeviceMode:[NSString stringWithFormat:#"No"]];
[self.view addSubview:ViewAddDevice.view];
}
In the .h file I have this.
#import <UIKit/UIKit.h>
#import "vcAddDevice.h"
#interface ViewControllerSettings : UIViewController<vcAddDeviceControllerDelegate, UITableViewDelegate, UITableViewDataSource>
- (IBAction) cmdBackClicked:(id) sender;
- (IBAction) cmdAddDeviceClicked:(id) sender;
#end

Navigation Controller Toolbar Buttons issue - Xcode

I'm trying to add some button to the toolbar of my navigationController: i see the toolbar, but without the buttons. This is the portion of my code where I set the toolbar...
(this is my AppDelegate)
// Create a table view controller
RootViewController *rootViewController = [[RootViewController alloc]
initWithStyle:UITableViewStyleGrouped];
rootViewController.managedObjectContext = context;
rootViewController.entityName = #"County";
//Navigation Controller
UINavigationController *aNavigationController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
//Barbuttons
UIBarButtonItem *homeButton;
homeButton = [[[UIBarButtonItem alloc] initWithTitle:#" Inizio " style:UIBarButtonItemStyleBordered target:self action:#selector(home)] autorelease];
UIBarButtonItem *barButton;
barButton = [[[UIBarButtonItem alloc] initWithTitle:#" Funzioni online " style:UIBarButtonItemStyleBordered target:self action:#selector(caricamappa)] autorelease];
UIBarButtonItem *creditsButton;
creditsButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"credits2.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(credits)] autorelease];
NSArray *baritems = [NSArray arrayWithObjects: homeButton, barButton, creditsButton, nil];
[window addSubview:[navigationController view]];
[self.navigationController.toolbar setItems:baritems];
[self.navigationController setToolbarHidden:NO];
[window makeKeyAndVisible];
[rootViewController release];
[aNavigationController release];
Any idea about my mistake?
You should add buttons to navigationItem property of your rootViewController, not to the toolbar of navigation controller.
Something like:
rootViewController.navigationItem.rightBarButtonItems = barItems;
Check out the documentation, especially this part:
Management of this toolbar’s contents is done through the custom view
controllers associated with this navigation controller. For each view
controller on the navigation stack, you can assign a custom set of
toolbar items using the setToolbarItems:animated: method of
UIViewController.

Resources