UIPickerView with UIToolBar issue - ios

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.

Related

How to get rid of the line on top of my button in a UIToolBar

I've created a UIToolBar and set two UIBarButtonItems and then assigned this toolbar to self.navigationItem.rightBarButtonItem.
Everything works perfectly except there is a line on top of my buttons.
The code:
UIBarButtonItem *addItem = [[UIBarButtonItem alloc] initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(addNewRow:)];
UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithTitle:#"Edit"
style:UIBarButtonItemStylePlain
target:self
action:#selector(editRow:)];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[toolBar setItems:#[addItem, editItem]];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolBar];
Please see my pic:
You get rid of the line by getting rid of the toolbar.
UIBarButtonItem *addItem = [[UIBarButtonItem alloc] initWithTitle:#"Add" style:UIBarButtonItemStylePlain target:self action:#selector(addNewRow:)];
UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStylePlain target:self action:#selector(editRow:)];
self.navigationItem.rightBarButtonItems = #[ addItem, editItem ];

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];

how to add two button in navigation bar with some space

I am adding two button in navigation bar they are working fine but i want space between them they are both combined i want a bit space between them
UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc] initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(Add)];
UIBarButtonItem *btnEdit = [[UIBarButtonItem alloc] initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(Add)];
UIToolbar *rightToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
rightToolBar.backgroundColor = [UIColor clearColor];
rightToolBar.tintColor = [UIColor colorWithRed:40.0/255.0 green:48.0/255.0 blue:51.0/255.0 alpha:0.0];
NSArray *buttonsRight = [NSArray arrayWithObjects:btnEdit, btnAdd, nil];
[rightToolBar setItems:buttonsRight];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightToolBar];
You can add any of these two between your UIBarButtonItem
UIBarButtonItem *fixed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]
Note that to set the width of a Fixed Space UIBarButtonItem, you need to set the .width property
[fixed setWidth:455.0f];

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];

iOS - UIToolBar as inputAccessoryView for UITextView

I've added a UIToolBar with a UIBarButtonItem as inputAccessoryView for a UITextView. It works fine but the UIBarButtonItem is touchable outside it's frame, perhaps 50 pixels outside to the right. It's no big deal but it annoys me. Anyone know why?
This is my code (ARC):
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.barStyle = UIBarStyleBlack;
toolBar.translucent = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneWriting:)];
[toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]];
self.messageTextView.inputAccessoryView = toolBar;
In iOS 6 it seems to behave as expected.
Nice tip: If you want the button to appear on the right instead of the left, use one of these:
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
Then initialise the toolbar with:
[toolBar setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, nil]];
The toolbar seems to expand the active area of the buttons beyond their bounds if there are no other nearby buttons in the toolbar. Apple engineers must think it is better to try to guess where the user intended to press rather than not react at all.
I hope it helps you...
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* PrevButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:105 target:nil action:nil]; //<
UIBarButtonItem* NextButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:106 target:nil action:nil]; //>
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneClicked:)];
UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *fake = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil] ;
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects: PrevButton,fake, NextButton,fake,flexSpace,fake,doneButton,nil] animated:YES];
Use Fake Item to get exact pinch location on Button...

Resources