cancel button appears but done button does not appear in uitoolbar - ios

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

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.

Pickerview in Actionsheet displaying in iPhone but not in iPad

Firstly, this is the pictures in iPhone and iPad
iPhone : http://img4.hostingpics.net/pics/563577Capturedcran20140610125811.png
iPad : http://img4.hostingpics.net/pics/315654Capturedcran20140610125901.png
This is how I am doing :
_questionnairePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0,44.0, 320.0, 250.0)];
_questionnaireActionSheet = [[UIActionSheet alloc] initWithTitle:#"Questionnaire"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
_questionnaireActionSheet.backgroundColor = [UIColor whiteColor];
_dateActionSheet.backgroundColor = [UIColor whiteColor];
self.selectedIndexQuestionnaire = 0;
self.questionnairePicker.delegate = self;
[self.questionnaireActionSheet addSubview:[self getToolBarActionSheet:self.questionnairePicker]];
[self.questionnaireActionSheet addSubview:self.questionnairePicker];
and
-(UIToolbar *)getToolBarActionSheet:(UIView *)aPicker
{
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(pickerDoneClick:)];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:#selector(pickerCancelClick:)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:nil];
if(aPicker.tag == 2) doneBtn.tag = cancelBtn.tag = 2;
[barItems addObject:cancelBtn];
[barItems addObject:flex];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
return pickerToolbar;
}
So, someone know a good way to resolve it ?
UIPickerViews on the ipad don't have a default size (ie width).
change
_questionnairePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0,44.0, 320.0, 250.0)];
to
_questionnairePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 345, 400, 216)];

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

UIPickerview not appearing at correct postion

Sorry for asking such a stupid question but I dont know why my UIPickerView with done button is not appearing at the correct place. I am using the following code but I dont know why I am getting such issue.
actionSheet=[[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
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(cancelButtonTapped:)];
[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(doneButtonTapped:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
//-----------
maritalStatusPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
maritalStatusPickerView.delegate = self;
maritalStatusPickerView.showsSelectionIndicator = YES;
[actionSheet addSubview:maritalStatusPickerView];
[actionSheet showInView:self];
use below code
actionSheet=[[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
actionSheet.frame = CGRectMake(0, 234, 320, 256);
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(cancelButtonTapped:)];
[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(doneButtonTapped:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
//-----------
maritalStatusPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
maritalStatusPickerView.delegate = self;
maritalStatusPickerView.showsSelectionIndicator = YES;
// [self addSubview:maritalStatusPickerView];
[actionSheet addSubview:maritalStatusPickerView];
[self.view addSubview:actionSheet];
Why you are adding maritalStatusPickerView 2 times
[self addSubview:maritalStatusPickerView];
[actionSheet addSubview:maritalStatusPickerView];
Just include the frame of actionsheet to your code after initialization
actionSheet.frame = CGRectMake(0, 0, 320, 256);
refer this question
you have to add your action sheet in your view, and change the co-Ordinate if you want to open pickerview from bottom of view.
[actionSheet addSubview:maritalStatusPickerView];
//Replace this : [actionSheet showInView:self];
[self.view addSubview:actionSheet];

Resources