How do you hide a UIToolbar? - ipad

I have 2 buttons within a toolbar. Is there a way to hide the toolbar and still show the 2 buttons? This is what have so far:
UIBarButtonItem *cameraButton = [[UIBarButtonItem alloc]
initWithTitle:#"Camera"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(useCamera:)];
/* UIBarButtonItem *cameraRollButton = [[UIBarButtonItem alloc]
initWithTitle:#"Camera Roll"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(useCameraRoll:)]; */
NSArray *items = [NSArray arrayWithObjects: cameraButton,
/* cameraRollButton*/ nil];
[toolbar setItems:items animated:YES];

The question's not very specific. You can remove the toolbar from its super view. You can set its opacity zero. You can set its frame off screen. You can animate the latter two.

Related

UIToolBar with UIBarButtonItems equally spaced regardless of title length

So I'm tasked with adding buttons to a UIToolBar and setting the inputAccessoryView of a textView to this UIToolBar.
so I did the basics:
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:#"title1" style:UIBarButtonItemStylePlain target:self action:selector];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"longer title2" style:UIBarButtonItemStylePlain target:self action:selector];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:#"title 3 is a lot longer" style:UIBarButtonItemStylePlain target:self action:selector];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *divider1 = [self barButtonDivider];
UIBarButtonItem *divider2 = [self barButtonDivider];
UIToolBar *toolBar = [UIToolBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[toolBar setItems:#[spacer,item1,spacer,divider1,spacer,item2,spacer,divider2,spacer,item3,spacer];
self.textView.inputAccessoryView = toolbar;
So there are more approaches to this.
1) I could subclass a UIToolBar
2) I could subclass a UIView and set a view to be the size I need (width of the superView and height 44), but I'd need to do all of this in Auto-Layout which I tried and could not get right.
Is there a way I can use the above method with a standard ToolBar and get the right spacing where all the buttons no matter how many I use are equal widths?
ETA:, tried the below solution and didn't work:
- (UIBarButtonItem *)spacer {
return [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
}
[toolBar setItems:#[[self spacer], item1, [self spacer], divider1, [self spacer], item2, [self spacer], divider2, [self spacer], item3, [self spacer]];
Resolved with this solution but I don't like it:
button.width = 150;
the buttons would remain equal in portrait or landscape so I guess this is a working solution what do you think?
I'm not sure, but issue might be in reusing spacer. Try to create new UIBarButtonSystemItemFlexibleSpace every time you need flexible space.

blank Toolbar from setItems

My UINavigationController toolbar is blank and no items show up on it. Here is my code
self.navigationController.toolbarHidden = NO;
UIToolbar* toolbar = self.navigationController.toolbar;
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"Test"
style:UIBarButtonItemStylePlain
target:self
action:#selector(buttonPushed)]];
[toolbar setItems:items animated:YES];
That's not the proper way to setup the toolbar. UIViewController has a toolbarItems property. The nav controller will use that property to automatically populate its toolbar.
Your code should be:
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *btnTest = [[UIBarButtonItem alloc] initWithTitle:#"Test"
style:UIBarButtonItemStylePlain
target:self
action:#selector(buttonPushed)]];
self.toolbarItems = #[ btnTest ];

UINavigationBar Item resizes after rotating

I set the buttons of my NavigationBar this way:
UIBarButtonItem *addAcc = [[UIBarButtonItem alloc]
initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(addNewAcc)];
UIBarButtonItem *delAcc = [[UIBarButtonItem alloc]
initWithTitle:#"Del"
style:UIBarButtonItemStylePlain
target:self
action:#selector(DeleteButtonAction)];
NSArray *arrBtns = [[NSArray alloc]initWithObjects:addAcc,delAcc, nil];
self.navigationItem.rightBarButtonItems = arrBtns;
This works well but after rotating the device or changing the buttons, they get much longer.
How can I solve this?
Regards
Here is a screenshot:
Before rotating : http://i.stack.imgur.com/9W3Hl.jpg
After rotating : http://i.stack.imgur.com/M27Hx.jpg
you should play with the property autoresizngmask of the buttons,
and autoriszessubViews .
check the refference :
autorizing

ios6 UINavigationBar with more than 2 buttons

in my app i have a uinavigationbar with 3 buttons, but the code i used for ios5 does not seem to be good for ios6 anymore.
You can see a wrong-colored rect underneath my custum-view with two buttons on the right side.
heres the code i used, i found it somewhere on stackoverflow:
UIToolbar *tools = [[[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 110.0f, 44.01f)] autorelease]; // 44.01 shifts it up 1px for some reason
tools.clearsContextBeforeDrawing = NO;
tools.clipsToBounds = NO;
tools.translucent = self.headerbar.translucent;
tools.barStyle = self.headerbar.barStyle;
tools.backgroundColor = self.headerbar.backgroundColor;
tools.tintColor = self.headerbar.tintColor;
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
// Create a standard refresh button.
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithTitle:#"heute" style:UIBarButtonItemStylePlain target:self action:#selector(showThisWeek)];
bi.style = UIBarButtonItemStyleBordered;
bi.width = 0;
[buttons addObject:bi];
//[bi release];
// Create a spacer.
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:#selector(showThisWeek)];
[buttons addObject:bi];
//[bi release];
// Add profile button.
bi = [[UIBarButtonItem alloc] initWithTitle:#"vor" style:UIBarButtonItemStylePlain target:self action:#selector(showNext)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
//[bi release];
// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
//[tools release];
header.rightBarButtonItem = twoButtons;
[header.leftBarButtonItem setAction:#selector(showPrevious)];
can someone hint me to a more elegant solution that works fine on both ios5 and ios6?
cheers
lukas
Did you try leftBarButtonItems and rightBarButtonItems? You can add array of buttons in left and right side nav bar using this.
If you use this, there will be no need for UIToolbar and their subviews.
Have you tried bar.style = -1?

How to add buttons to the top bar of a detail view from a UITableView

When an item from a UITableView is selected, a detail view is loaded which has a banner bar at the top with a back button on it to navigate back to the table.
How do I add other buttons to that banner bar?
As Andrew said, you can add custom views to the navigation bar. For example, if you are looking to add multiple buttons to the right side of the navigation bar, you can do something like this:
// right side of nav bar
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 106, 44)];
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
[deleteButton release];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelAction:)];
cancelButton.style = UIBarButtonItemStylePlain;
[buttons addObject:cancelButton];
[cancelButton release];
[toolbar setItems:buttons animated:NO];
toolbar.barStyle = -1;
[buttons release];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
[toolbar release];
Make sure to adjust the width (106 above) if you need more or wider buttons, and then provide methods for the selectors (deleteAction: and cancelAction: above).
From your description, it sounds like you have a UINavigationController correctly set up. That "banner bar" you are describing is properly called a navigation bar.
From within the detail view, you can use:
UIBarButtonItem* button = [[[UIBarButtonItem alloc] initWithTitle:"HiMom" style:UIBarButtonItemStylePlain target:self action:#selector(onHiMom:)] autorelease];
self.navigationItem.rightBarButtonItem = button;
You can also add custom views (instead of a button) plus a few other settings. I suggest poking around the documentation for UINavigationItem, UINavigationBar, and UIBarButtonItem for ideas.

Resources