Custom UIActionSheet squished on iPad - ios

I have read through several similar questions here on stack but so far none of the answers seem to fit. Therefore it must be something so blatantly obvious that I will have to flog myself later once it gets answered.
The code works fine on iPhone but not iPad, the action sheet has little to no height. There is no tool bar and it is fired off a normal UIButton. I have considered a popover however this is a universal app so I would rather not re-wright the same code twice for both iPhone and iPad and try and keep the app as light as possible.
I have tried showFromRect and showFromTabbar.
Thanks
Code:
-(IBAction) setDateTime:(id) sender {
dateSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[dateSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 44, 0, 0);
UIDatePicker *appointmentDayPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[appointmentDayPicker setDatePickerMode:UIDatePickerModeDateAndTime];
[dateSheet addSubview:appointmentDayPicker];
[appointmentDayPicker release];
UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleBlack];
[controlToolBar sizeToFit];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:#"Set" style:UIBarButtonItemStyleDone target:self action:#selector(dismissDateSet)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelDateSet)];
[controlToolBar setItems:[NSArray arrayWithObjects:spacer, cancelButton,setButton, nil] animated:NO];
[spacer release];
[setButton release];
[cancelButton release];
[dateSheet addSubview:controlToolBar];
[controlToolBar release];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
[dateSheet showFromRect:CGRectMake(100,100, 320, 485) inView:self.view animated:YES];
[dateSheet setBounds:CGRectMake(0, 0, 200, 485)];
} else {
[dateSheet showInView:self.view];
[dateSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
}
EDIT: Adding pic

Related

DatePicker does not show on input click on iOS 8

I am showing pickerView on input field click it worked fine on other iOS but when I updated to iOS 8 it did not work. When i click on input field it does not show up the date picker.
Here is the code I am using.
- (void)textFieldDidBeginEditing:(UITextField *)aTextField{
appDelegate.recentAddSowID = #"";
if (aTextField == dateTextField) {
[dateTextField resignFirstResponder];
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"" destructiveButtonTitle:nil otherButtonTitles:nil];
pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode=UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar 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(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerView addTarget:self action:#selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
[dateTextField setInputView:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
}
}
I am testing on iPhone 5S.
They changed the implementation of UIActionSheet in iOS 8. You can no longer add views to its view hierarchy
The documentation (https://developer.apple.com/library/ios/documentation/uikit/Reference/UIActionSheet_Class/index.html) states:
UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:.
Also, the UIActionSheet is deprecated in iOS 8
Important: UIActionSheet is deprecated in iOS 8. (Note that UIActionSheetDelegate is also deprecated.) To create and manage action sheets in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet.
As an alternative, try this: https://github.com/skywinder/ActionSheetPicker-3.0. It works with iOS 8 and might be what you need
Source: Add UIPickerView in UIActionSheet from IOS 8 not working

UIDatePicker error on dateSheet showInView

I need to show a datePicker when a textField is clicked.
Here is the code:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self setDate];
return NO;
}
-(void)setDate{
dateSheet = [[UIActionSheet alloc] initWithTitle:Nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[dateSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 44, 0, 0);
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
NSDate * maxDate = [NSDate dateWithTimeIntervalSinceNow:(3600 * 24 * 14)];
datePicker.maximumDate = maxDate;
datePicker.minimumDate = [NSDate date];
[dateSheet addSubview:datePicker];
UIToolbar * controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleBlack];
[controlToolBar sizeToFit];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:#"Imposta" style:UIBarButtonItemStyleDone target:self action:#selector(dismissDate)];
setButton.tintColor = [UIColor whiteColor];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Annulla" style:UIBarButtonItemStyleDone target:self action:#selector(cancelDate)];
cancelButton.tintColor = [UIColor whiteColor];
[controlToolBar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil] animated:NO];
[dateSheet addSubview:controlToolBar];
[dateSheet showInView:self.view];
[dateSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
When showInView is called, a lot of errors are shown in console:
CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Then the same message with:
CGContextSetStrokeColorWithColor
CGContextSaveGState
CGContextSetFlatness
CGContextAddPath
CGContextDrawPath
CGContextRestoreGState
Why this happens?
After running your code, I found the problem is caused by the nil title of UIActionSheet.
A workaround is modified as below.
dateSheet = [[UIActionSheet alloc] initWithTitle:#"" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
Another good resource for your reference.

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

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

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