Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing a UIPickerView with UIActionSheet in iOS8 not working

Showing a UIPickerView with UIActionSheet in iOS8 not working

The code works in iOS7, however it is not working in iOS8. I'm sure it is because UIActionSheet is deprecated in iOS8 and Apple recommends to use UIAlertController.

However, how to do it in iOS8? I should use UIAlertController?

iOS7:
enter image description here

iOS8:
enter image description here

EDIT:

My GitHub project solving the problem.

like image 253
Gabriel.Massana Avatar asked Jun 20 '14 14:06

Gabriel.Massana


4 Answers

I write the time picker by my self instead UIActionSheet in iOS8:

date = [NSDate date];

timePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
timePicker.datePickerMode = UIDatePickerModeDateAndTime;
timePicker.hidden = NO;
timePicker.date = date;

displayFormatter = [[NSDateFormatter alloc] init];
[displayFormatter setTimeZone:[NSTimeZone localTimeZone]];
[displayFormatter setDateFormat:@"MM月dd日 EEE HH:mm"];

formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

startDisplayTimeString = [displayFormatter stringFromDate:timePicker.date];
startTimeString = [formatter stringFromDate:timePicker.date];

NSTimeInterval interval = 24*60*60*1;
NSDate *endDate = [[NSDate alloc] initWithTimeIntervalSinceNow:interval];
endDisplayTimeString = [displayFormatter stringFromDate:endDate];
endTimeString = [formatter stringFromDate:endDate];

[_startTimeLabel setText:startDisplayTimeString];
[_endTimeLabel setText:endDisplayTimeString];

[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];

NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
pickerToolbar.tintColor = [UIColor whiteColor];
[pickerToolbar sizeToFit];

UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelBtnPressed:)];

[cancelBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                   [UIColor colorWithRed:253.0/255.0 green:68.0/255.0 blue:142.0/255.0 alpha:1.0],
                                   NSForegroundColorAttributeName,
                                   nil] forState:UIControlStateNormal];

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

UIBarButtonItem *titleButton;

float pickerMarginHeight = 168;


titleButton = [[UIBarButtonItem alloc] initWithTitle:@"title" style:UIBarButtonItemStylePlain target: nil action: nil];

[titleButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                     [UIColor colorWithRed:253.0/255.0 green:68.0/255.0 blue:142.0/255.0 alpha:1.0],
                                     NSForegroundColorAttributeName,
                                     nil] forState:UIControlStateNormal];

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"OK" style:UIBarButtonItemStyleDone target:self action:@selector(setTimePicker)];

[doneBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                 [UIColor colorWithRed:253.0/255.0 green:68.0/255.0 blue:142.0/255.0 alpha:1.0],
                                 NSForegroundColorAttributeName,
                                 nil] forState:UIControlStateNormal];

NSArray *itemArray = [[NSArray alloc] initWithObjects:cancelBtn, flexSpace, titleButton, flexSpace, doneBtn, nil];

[pickerToolbar setItems:itemArray animated:YES];

if(iPad){

    [pickerToolbar setFrame:CGRectMake(0, 0, 320, 44)];

    UIViewController* popoverContent = [[UIViewController alloc] init];
    popoverContent.preferredContentSize = CGSizeMake(320, 216);
    UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
    popoverView.backgroundColor = [UIColor whiteColor];
    [popoverView addSubview:timePicker];
    [popoverView addSubview:pickerToolbar];
    popoverContent.view = popoverView;
    popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
    [popoverController presentPopoverFromRect:CGRectMake(0, pickerMarginHeight, 320, 216) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

}else{

    timeBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-300, 320, 246)];
    [timeBackgroundView setBackgroundColor:[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0]];

    [timeBackgroundView addSubview:pickerToolbar];
    [timeBackgroundView addSubview:timePicker];

    [self.view addSubview:timeBackgroundView];}
like image 34
folse Avatar answered Nov 20 '22 17:11

folse


From the reference for UIActionSheet:

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:.

My guess is your seeing exactly why.

The reference for UIAlertController doesn't have a similar disclaimer, but looking at its interface, my guess is Apple will add it before release.

My recommendation would be to just create a small view containing your picker and buttons and show and hide it as needed. It's not that hard to do and your not pushing interfaces beyond their intended uses.

like image 171
David Berry Avatar answered Nov 20 '22 15:11

David Berry


UIActionSheet is depricated in iOS8. Use UIAlertController for iOS8 or later

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                            message:@"This is an action shhet."
                            preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* cameraAction = [UIAlertAction actionWithTitle:@"Take A Photo" style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action) {}];

UIAlertAction* galleryAction = [UIAlertAction actionWithTitle:@"From Gallery" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action) {}];

UIAlertAction * defaultAct = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
                                                    handler:^(UIAlertAction * action) {}];

[alert addAction:cameraAction];
[alert addAction:galleryAction];
[alert addAction:defaultAct];

[self presentViewController:alert animated:YES completion:nil];

Ref : UIAlertController

Example : https://github.com/KiritVaghela/UIAlertController

like image 3
Kirit Vaghela Avatar answered Nov 20 '22 17:11

Kirit Vaghela


Try to use this action sheet simulation:

@interface UIViewController (ActionSheetSimulation)

-(UIView*) actionSheetSimulationWithPickerView:(UIPickerView*)pickerView withToolbar: (UIToolbar*)pickerToolbar;
-(void)dismissActionSheetSimulation:(UIView*)actionSheetSimulation;

@end


#import "UIViewController+ActionSheetSimulation.h"

@implementation UIViewController (ActionSheetSimulation)

-(UIView *)actionSheetSimulationWithPickerView:(UIPickerView *)pickerView withToolbar:(UIToolbar *)pickerToolbar {

    UIView* simulatedActionSheetView = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                                                0,
                                                                                UIScreen.mainScreen.bounds.size.width,
                                                                                UIScreen.mainScreen.bounds.size.height)];
    [simulatedActionSheetView setBackgroundColor:[UIColor clearColor]];

    CGFloat pickerViewYpositionHidden = UIScreen.mainScreen.bounds.size.height + pickerToolbar.frame.size.height;
    CGFloat pickerViewYposition = UIScreen.mainScreen.bounds.size.height -
        pickerView.frame.size.height +
        UIApplication.sharedApplication.statusBarFrame.size.height;
    [pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
                                pickerViewYpositionHidden,
                                pickerView.frame.size.width,
                                pickerView.frame.size.height)];
    [pickerToolbar setFrame:CGRectMake(pickerToolbar.frame.origin.x,
                                   pickerViewYpositionHidden,
                                   pickerToolbar.frame.size.width,
                                   pickerToolbar.frame.size.height)];
    pickerView.backgroundColor = [UIColor whiteColor];
    [simulatedActionSheetView addSubview:pickerToolbar];
    [simulatedActionSheetView addSubview:pickerView];

    [UIApplication.sharedApplication.keyWindow?UIApplication.sharedApplication.keyWindow:UIApplication.sharedApplication.windows[0]
                                                                            addSubview:simulatedActionSheetView];
    [simulatedActionSheetView.superview bringSubviewToFront:simulatedActionSheetView];

    [UIView animateWithDuration:0.25f
                 animations:^{
                         [simulatedActionSheetView setBackgroundColor:[[UIColor darkGrayColor] colorWithAlphaComponent:0.5]];
                         [self.view setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed];
                         [self.navigationController.navigationBar setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed];
                         [pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
                                                     pickerViewYposition,
                                                     pickerView.frame.size.width,
                                                     pickerView.frame.size.height)];
                         [pickerToolbar setFrame:CGRectMake(pickerToolbar.frame.origin.x,
                                                        pickerViewYposition,
                                                        pickerToolbar.frame.size.width,
                                                        pickerToolbar.frame.size.height)];
                     }
                     completion:nil];

    return simulatedActionSheetView;
}

-(void)dismissActionSheetSimulation:(UIView*)actionSheetSimulation {
    [UIView animateWithDuration:0.25f
                 animations:^{
                         [actionSheetSimulation setBackgroundColor:[UIColor clearColor]];
                         [self.view setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];
                         [self.navigationController.navigationBar setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];
                         [actionSheetSimulation.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                             UIView* v = (UIView*)obj;
                             [v setFrame:CGRectMake(v.frame.origin.x,
                                                UIScreen.mainScreen.bounds.size.height,
                                                v.frame.size.width,
                                                v.frame.size.height)];
                         }];
                     }
                     completion:^(BOOL finished) {
                         [actionSheetSimulation removeFromSuperview];
                     }];

}
@end
like image 2
Bulky_ooti Avatar answered Nov 20 '22 15:11

Bulky_ooti