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
Related
I'm trying to add Previous/Next buttons above the keyboard like in mobile Safari. There are a lot of questions about that here on StackOverflow, but most answers are to use inputAccessoryView.
I tried that and it looks like this:
Is there anyway to have the buttons in the toolbar bellow, like it works in mobile Safari??
This is how it looks like in Safari:
It seems what I was looking for is inputAssistantItem.
It allows you to add and change buttons in the keyboard's shortcut bar.
UIBarButtonItem* nextButton = [[UIBarButtonItem alloc]
initWithImage:nextImage
style:UIBarButtonItemStylePlain
target:self
action:#selector(nextField:)];
UIBarButtonItem* previousButton = [[UIBarButtonItem alloc]
initWithImage:previousImage
style:UIBarButtonItemStylePlain
target:self
action:#selector(previousField:)];
NSArray *buttonGroups = #[[[UIBarButtonItemGroup alloc]
initWithBarButtonItems:#[previousButton, nextButton]
representativeItem:nil]];
textField.inputAssistantItem.trailingBarButtonGroups = buttonGroups;
UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:103 target:self action:#selector(previousButtonTapped:)];
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:6.0];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:104 target:self action:#selector(nextButtonTapped:)];
Why would my UIBarButtonItem sometimes disappear?
When you first load the screen (the first in a Tab Bar) it displays fine. However when you navigate to a new screen and then return both left and right buttons disappear. They aren't reacting to touch events either. However, if you go to a second tab and return they suddenly appear.
It must be a recent iOS change 'cos it was working previously.
==== UPDATE
The code is hardly rocket science but here it is:
Screen 1:
In viewDidLoad:
UIImage *myImage = [UIImage imageNamed:#"image.png"];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:myImage
style:UIBarButtonItemStyleBordered
target:self
action:#selector(myAction:)];
self.navigationItem.rightBarButtonItem = item;
and
- (IBAction)myAction:(id) sender {
MyController *mc = [[MyController alloc] initWithNibName:#"MyController" bundle:nil];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Title" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(pop)];
self.navigationItem.backBarButtonItem = barBtnItem;
I placed the above code in iphone it is working.when i am trying it for ipad it is not working.what may be reason?
Thanks in Advance
This changes NavigationBar's BackButton text, to do before pushing a new view controller :
UIViewController *myViewController = [[UIViewController alloc] init];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"My text" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[self.navigationController pushViewController:myViewController animated:YES];
And it works for both iPhone and iPad devices.
It looks like you linked your UINavigationController with Interface Builder, you should have a look at this :
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.
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];