Adding multiple buttons to UINavigationController - ios

I have the following code that adds some buttons to the navigation controller. I need this so that I can have a toolbar with multiple items on it.
CGRect frame = self.navigationController.navigationBar.frame;
frame.size.height += 0.01f;
UIToolbar *tb= [[UIToolbar alloc] initWithFrame:frame];
NSMutableArray *tbitems = [NSMutableArray array];
tb.autoresizingMask= UIViewAutoresizingFlexibleWidth;
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[tbitems addObject:bi];
[bi release];
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[tbitems addObject:bi];
[bi release];
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[tbitems addObject:bi];
[bi release];
tb.items = tbitems;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tb];
[tb release];
This works great in Portrait however in Landscape it does not work so well! There is a gap at the end and I cannot work out how to get it to fill the gap!

Related

NSInvalidArgumentException', reason: '-[UILabel view]: unrecognized selector sent to instance 0x7ffbd0da2680'

Below is the code where i get this error.
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, self.view.bounds.size.height - 44.0f, self.view.bounds.size.width, 44.0f)];
toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:toolBar];
UIBarButtonItem *people = [[UIBarButtonItem alloc] initWithTitle:#"people" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *food = [[UIBarButtonItem alloc] initWithTitle:#"food" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *nature = [[UIBarButtonItem alloc] initWithTitle:#"nature" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *sports = [[UIBarButtonItem alloc] initWithTitle:#"sports" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *cats = [[UIBarButtonItem alloc] initWithTitle:#"cats" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UILabel *lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(0,5,100,20)];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor whiteColor];
lbl1.userInteractionEnabled=YES;
[self.view addSubview:lbl1];
lbl1.text= #"TEST";
[toolBar setItems:#[space, lbl1, people, food, nature, sports, cats, space]];
Your mistake is in this line because you add label in toolBar. Use this code
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, self.view.bounds.size.height - 44.0f, self.view.bounds.size.width, 44.0f)];
toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:toolBar];
UIBarButtonItem *people = [[UIBarButtonItem alloc] initWithTitle:#"people" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *food = [[UIBarButtonItem alloc] initWithTitle:#"food" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *nature = [[UIBarButtonItem alloc] initWithTitle:#"nature" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *sports = [[UIBarButtonItem alloc] initWithTitle:#"sports" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *cats = [[UIBarButtonItem alloc] initWithTitle:#"cats" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(0,64,100,20)];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor redColor];
lbl1.userInteractionEnabled=YES;
[self.view addSubview:lbl1];
lbl1.text= #"TEST";
[toolBar setItems:#[space, people, food, nature, sports, cats, space]];
toolBar item clicked event
-(IBAction)tap:(UIBarButtonItem*)sender{
lbl1.text= sender.title;
}

UIPickerView with UIToolBar issue

I'm having an issue with a UIPickerView with a UIToolBar that has a Done button. The thing is that the button Done sometimes is showed in blue (normal behavior) and other times the button appear in a sheer grey that is difficult to see (See picture attached).
The pickerview and the toolbar are generated this way:
mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
[mypickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(pickerDoneClicked:)];
[barItems addObject:doneBtn];
[mypickerToolbar setItems:barItems animated:YES];
statusTextField.inputAccessoryView = mypickerToolbar;
I've searched about it with no luck.

Flexible bar button item now displayed on UIToolbar

I know it may seem very simple and has been asked many times but i can't get it to work .I want to add two buttons to my toolbar. One on the right side and the other one on the left. Here is the code but the flexible one which is supposed to show up on the right side doesn't appear at all. Here is the code:
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"next" style:UIBarButtonItemStylePlain target:self action:#selector(nextResult:)]];
UIBarButtonItem *done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:#selector(showSearch:)];
[done setImage:[UIImage imageNamed:#"cancel"]];
[items addObject:done];
[toolbar setItems:items animated:NO];
You need to use UIBarButtonSystemItemFlexibleSpace instead of UIBarButtonSystemItemFixedSpace
Code:
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"next" style:UIBarButtonItemStylePlain target:self action:#selector(nextResult:)]];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"cancel" style:UIBarButtonItemStylePlain target:self action:#selector(showSearch:)]];
[toolbar setItems:items animated:NO];
Reference : UIBarButtonItem Class Reference
The item with the system item UIBarButtonSystemItemFixedSpace is a space, not button object. Therefore it does show up. It's just no button.
Change your code to
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"next" style:UIBarButtonItemStylePlain target:self action:#selector(nextResult:)]];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"cancel" style:UIBarButtonItemStylePlain target:self action:#selector(showSearch:)]];
[toolbar setItems:items animated:NO];

UITextField delegate causes UIToolBar not show in iOS

This is what I am doing to display a UIToolbar in a UITextField's acceccory view.. Unfortunately I am not able to see it for some reason. What am I doing wrong here?
UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneTyping)];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered
target:self action:#selector(gotoNextTextfield:)];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
NSArray *items = [NSArray arrayWithObjects:item2, flexiableItem, item1, nil];
toolbar.items = items;
[inputAccView addSubview:toolbar];
[self.msgTextField setInputAccessoryView:inputAccView];
Update:
if I remove self.msgTextField.delegate = self; then I can see the toolbar.. but why??
Try replacing
[inputAccView addSubview:toolbar];
[self.msgTextField setInputAccessoryView:inputAccView];
with
[self.msgTextField setInputAccessoryView:toolbar];
In your .h file add <UITextFieldDelegate>
Then in your - (void)viewDidLoad try This, it works fine for me:
// Keyboard Tool Bar
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
[toolbar sizeToFit];
UIBarButtonItem *nextField =[[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextField)];
UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(resignKeyboard)];
NSArray *itemsArray = [NSArray arrayWithObjects: nextField, flexButton, doneButton, nil];
[toolbar setItems:itemsArray];
[self.msgTextField setInputAccessoryView:toolbar];

cancel button appears but done button does not appear in uitoolbar

I have written the code below to display the datepicker in popover along uitoolbar.
I can see the cancel button but not the Done button.
If I remove the code for flexspace then done button appears but I need the done button to be on the extreme right positon but it stands next to cancel button.
How can I fix this ? Thanks in Advance.
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
datePicker=[[UIDatePicker alloc]init];//Date picker
datePicker.frame=CGRectMake(0,44,320, 216);
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
[datePicker addTarget:self action:#selector(Result) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate=self;
[popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
[popoverController presentPopoverFromRect:self.uitext.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];//tempButton.frame where you need you can put that frame
// UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320,40)];
// [pickerToolbar sizeToFit];
// pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
// NSMutableArray *barItems = [[NSMutableArray alloc] init];
//
// UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
// [barItems addObject:cancelBtn];
//
// UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// [barItems addObject:flexSpace];
//
// UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
// [barItems addObject:doneBtn];
//
//
//
// [pickerToolbar setItems:barItems animated:YES];
//
// UIPickerView *picker = [[UIPickerView alloc] init];
// picker.frame = CGRectMake(0, 44, 320, 216);
// picker.delegate = self;
// picker.dataSource = self;
// picker.showsSelectionIndicator = YES;
// [actionSheet addSubview:picker];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,40)];
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[popoverView addSubview:pickerToolbar];
[self.uitext resignFirstResponder];
Try this code :-
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,40)];
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[popoverView addSubview:pickerToolbar];
Hope it helps you..
EDIT :-
Add this line below [pickerToolbar setItems:barItems animated:YES]; :-
pickerToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
and comment [pickerToolbar sizeToFit]; line
So your code will be
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,44.0)];
//[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
pickerToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
[popoverView addSubview:pickerToolbar];

Resources