UIPickerview not appearing at correct postion - ios

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

Related

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

How to modify a Done button programmatically from UIActionSheet?

I have this method to create an action sheet and uipicker:
-(void)presentNewPicker{
self.aac = [[UIActionSheet alloc] initWithTitle:#"What City?"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:#"OK", nil];
self.pickrView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
self.pickrView.showsSelectionIndicator = YES;
self.pickrView.dataSource = self;
self.pickrView.delegate = self;
self.cityNames = [[NSArray alloc] initWithObjects: #"Phoenix", #"Tahoe", #"Nevada", #"Lime", #"Florida", nil];
UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheet:)];
[barItems addObject:doneBtn];
[pickerDateToolbar setItems:barItems animated:YES];
[self.aac addSubview:pickerDateToolbar];
[self.aac addSubview:self.pickrView];
[self.aac showInView:self.view];
[self.aac setBounds:CGRectMake(0,0,320, 464)];
}
The button that appears reads DONE but I want to modify it to read, Ready. How do I accomplish this?
Replace:
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheet:)];
with:
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Ready" style: UIBarButtonItemStyleBordered target:self action:#selector(dismissActionSheet:)];
You may want to use UIBarButtonItemStyleDone for the style.
I don't know what you want to do, but you might be interested in using the inputView and inputAccessoryView for one of your uiviews, to replace the keyboard. That'll give you the animations for free, and a place to put a toolbar. Some answers that might help: https://stackoverflow.com/a/6075062/220820
https://stackoverflow.com/a/8583703/220820

Why not see the buttons?

The task is to choose the date for a text field. The development is on the iPad, so I use UIPopover. But I need two buttons on top. I'm trying to do it, but the buttons are not displayed. In what could be the problem?
Please see screenshot:
And the full code:
dateSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[dateSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 44, 0, 0);
UIDatePicker *dayPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[dayPicker setDatePickerMode:UIDatePickerModeDate];
[dateSheet addSubview:dayPicker];
[dayPicker release];
UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleBlackTranslucent];
[controlToolBar sizeToFit];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton;
setButton = [[UIBarButtonItem alloc] initWithTitle:#"Установить" style:UIBarButtonItemStyleDone target:self action:#selector(dismissDateStart:)];
setButton.tag = pTag;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Отменить" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelDateStart)];
[controlToolBar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil] animated:NO];
[spacer release];
[setButton release];
[cancelButton release];
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 344)];
popoverView.backgroundColor = [UIColor whiteColor];
dateSheet.frame = CGRectMake(0, 0, 320, 344);
[popoverView addSubview:dateSheet];
[popoverView addSubview:controlToolBar];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 244);
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController presentPopoverFromRect:changeDateStartField.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[popoverView release];
[popoverContent release];
You should change the
UIBarButtonSystemItemFlexibleSpace
to
UIBarButtonSystemItemFixedSpace
of your spacer button

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

Pickerview for TextField

I have this code, it's inside an action that it's triggered when the user selects the textfield to show the datepicker:
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDateAndTime;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar 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(doneButtonPressed:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showFromTabBar:self.tabBarController.tabBar];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 500)];
I was using this code in an app with a tab bar for navigation. Now I would like to use it in an app without it, but when the process reaches [pickerViewPopup showFromTabBar:self.tabBarController.tabBar]; it crashes.
Any idea on how I could fix it?
Use one of the following,
– showFromToolbar:
– showInView:
– showFromBarButtonItem:animated:
– showFromRect:inView:animated:
For eg:-
[pickerViewPopup showFromToolbar:pickerToolbar];
The problem was that you dont have a tab bar and you are trying to access the tab bar object to show the picker from it. Since tabbar is nil for you, it will crash.
For more details check the apple documentation.

Resources