LeftBarButtonItem with two buttons. One Button doesnt respond to tap - ios

So I have two buttons in the leftBarButtonItem. This is my code.
NSString *todayString = #"...";
UIBarButtonItem *todayButton = [[[UIBarButtonItem alloc] initWithTitle:todayString style:UIBarButtonItemStyleBordered target:self.calendarController action:#selector(todaySelected)] autorelease];
NSString *weeklyString = #"...";
UIBarButtonItem *weeklyButton = [[[UIBarButtonItem alloc] initWithTitle:weeklyString style:UIBarButtonItemStyleBordered target:self action:#selector(weekSelected)] autorelease];
UIToolbar *toolbar = [[[UIToolbar alloc] init] autorelease];
[toolbar setItems:[NSArray arrayWithObjects:todayButton, weeklyButton, nil]];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease];
The title of the buttons is dynamic. Sometimes the titles can get somewhat long. The buttons expand and look fine. However, if the titles get too long, it seems that the UIToolbar reaches some width maximum, as the buttons stop responding to taps at a certain X point. See image here
Green represents responds to touch and red does not respond to touch. The Today button responds to touch. The Weekly button, however, only responds to touch until the second "e". Anything after that does not respond to touch.
I've been banging my head trying to fix this for a while, with no luck. I tried expanding the frame of both the UIToolbar and the leftBarButtonItem. Anyone know whats going on? Thanks in advance

Your toolbar frame width is probably too narrow. In your code, you don't even set its size, and rely on it to be sized it for you. Trying setting it to larger width, and make sure the autoresizingMask does not have `UIViewAutoresizingFlexibleWidth' set.
A good debugging technique is to set the background color of your new toolbar to something bright (to contrast against the toolbar you are installing it into) and watch its sizing to see how your button is being clipped interaction-wise.
You may need to explicitly resize it as you update the bar button item titles if fixing it to a certain width is too restrictive for your app.

You'll need to make a custom button view that is actionable, with something like a UIButton:
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[newButton addTarget:self action:#selector(newAction:) forControlEvents:UIControlEventTouchUpInside];
[newButton setTitle:#"New Button" forState:UIControlStateNormal];
UIBarButtonItem *newBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:newButton];
self.navigationItem.leftBarButtonItem = newBarButtonItem;
for example.
EDIT - now I see what you are doing, I apologize for the earlier confusion. You are adding an array of buttons to the left side. Try adjusting the frame of the toolbar with so the buttons fit inside the interactive area:
NSString *todayString = #"...";
UIBarButtonItem *todayButton = [[UIBarButtonItem alloc] initWithTitle:todayString style:UIBarButtonItemStyleBordered target:self action:nil];
NSString *weeklyString = #"...";
UIBarButtonItem *weeklyButton = [[UIBarButtonItem alloc] initWithTitle:weeklyString style:UIBarButtonItemStyleBordered target:self action:nil];
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setFrame:CGRectMake(0, 0, 320, 50)];
[toolbar setItems:[NSArray arrayWithObjects:todayButton, weeklyButton, nil]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];

I know this is an old question, but it led me astray for a bit until I found updated information. I could not use the technique mentioned above in iOS 7 because the embedded toolbar would not line up with its parent.
Anyhow, iOS 5 (I believe) added button arrays for the left and right items:
UIBarButtonItem * trashItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:#selector(onTrashClick:)];
UIBarButtonItem * mailItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(onActionClick:)];
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects: refreshBtn, selectYearBtn, nil];

Related

move menu button slightly right

I want to have a controller with both back button and menu button as in given image.amazon app
But the problem is the even after increasing the x-axis value it does not shift to right. Hence, I am unable to add back button image. Here's what I am doing right now.
button = [[UIButton alloc] initWithFrame:CGRectMake(200, 100, 100, 25)];
[button setImage:[UIImage imageNamed:#"menu.png"] forState:UIControlStateNormal];
[button addTarget:[SlideNavigationController sharedInstance] action:#selector(toggleLeftMenu) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[SlideNavigationController sharedInstance].leftBarButtonItem = rightBarButtonItem;
Try this,
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(back)];
UIBarButtonItem *btnMenu = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(menu)];
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:btnBack, btnMenu, nil]];
Well on your code you specifically replaced the back button with a barBarButtonItem.
BTW, your naming is a bit confusing you are assigning a rightBarButtonItem to the leftBarButtonItem of the navigationController.
[SlideNavigationController sharedInstance].navigationItem.leftBarButtonItems= #[leftButton,menuButton]; // this will assign both button on the left side. same thing for rightBarButtonItems

UIToolbar left and right paddings are initially negative

I'm struggling with UIToolbar's strange behaviour in iOS7+ iPhone application.
Here's how I created it:
//toolbar
self.bottomToolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0.0, self.view.bounds.size.height-44.0, self.view.bounds.size.width, 44.0)];
self.bottomToolbar.autoresizingMask=(UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth);
[MKUIHelper toolbar:self.bottomToolbar setBarStyle:MKBarStyleWhite];
self.bottomToolbar.items=#[];
[self.view addSubview:self.bottomToolbar];
When I need to update it I'm calling (actually updating only one of its items but this does not matter):
-(void)updateToolbar
{
UIBarButtonItem *doneBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:#"done" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonDidPress:)];
doneBarButtonItem.tintColor=MK_Color_Green;
UIBarButtonItem *clearBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:#"clear" style:UIBarButtonItemStylePlain target:self action:#selector(clearButtonDidPress:)];
clearBarButtonItem.tintColor=MK_Color_Pink;
UIBarButtonItem *spaceBetweenBackAndQuantity=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *spaceBetweenQuantityAndClear=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *quantityItem=[self quantityBarButtonItem];
self.bottomToolbar.items=[NSArray arrayWithObjects:doneBarButtonItem, spaceBetweenBackAndQuantity, quantityItem, spaceBetweenQuantityAndClear, clearBarButtonItem, nil];
[self.rootViewController reloadToolbar];
}
Both UIBarButtonItem objects that was created using initWithTitle:.. have negative left and right paddings somehow. It can be roughly fixed by adding additional fixed space type UIBarButtonItems to both sides of the items array. In other words: compensate strange initial left and right paddings with fixed space type UIBarButtonItems.
Help me someone, please. What causes such strange effect? How can I get rid of this strange padding?
Here's how it looks like:
If you set the width of barButtons to correct width, you may be able to work around this.
- (UIBarButtonItem *)barButtonWithTitle:(NSString *)title action:(SEL)action {
UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action] autorelease];
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setTitle:title forState: UIControlStateNormal];
[button sizeToFit];
CGSize size = button.bounds.size;
[barButtonItem setWidth:size.width];
return barButtonItem;
}

How to show Done on Decimal Pad?

I have a UITextField which is Number Pad and its properties look like
But when I run my application, I do not see Done on my screen
What is the issue? How can I get Done on Decimal Pad?
The best way I've found when doing something like this is to append an inputAccessoryView to the UITextField. That input accessory view can have a "Done" button on it that dismisses the keyboard for you. I don't think you should put a "Done" button on the key pad itself, because where would it go?
Here's how I've done it before:
- (void)setupKeyboardAccessory {
UIToolbar *keyboardToolbar = [[UIToolbar alloc] init];
[keyboardToolbar sizeToFit];
keyboardToolbar.backgroundColor = [UIColor whiteColor];
UIBarButtonItem *flexBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self.patientAgeTextField action:#selector(resignFirstResponder)];
keyboardToolbar.items = #[flexBarButton, doneBarButton];
self.textField.inputAccessoryView = keyboardToolbar;
}

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