ios 在 iOS8 中显示带有 UIActionSheet 的 UIPickerView 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24330152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing a UIPickerView with UIActionSheet in iOS8 not working
提问by Gabriel.Massana
Showing a UIPickerView
with UIActionSheet
in iOS8not working
在iOS8 中显示UIPickerView
with不起作用UIActionSheet
The code works in iOS7, however it is not working in iOS8. I'm sure it is because UIActionSheet is deprecated in iOS8and Apple recommends to use UIAlertController
.
该代码适用于iOS7,但不适用于iOS8。我确定这是因为 UIActionSheet 在iOS8 中已被弃用,而 Apple 建议使用UIAlertController
.
However, how to do it in iOS8? I should use UIAlertController
?
但是,如何在iOS8 中做到这一点?我应该使用UIAlertController
?
iOS7:
iOS7:
iOS8:
iOS8:
EDIT:
编辑:
采纳答案by David Berry
From the reference for UIActionSheet:
来自 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:.
UIActionSheet 并非设计为子类化,也不应将视图添加到其层次结构中。如果您需要呈现比 UIActionSheet API 提供的自定义更多的表单,您可以创建自己的表单并使用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.
UIAlertController 的参考文献没有类似的免责声明,但是看它的界面,我猜苹果会在发布之前添加它。
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.
我的建议是只创建一个包含选择器和按钮的小视图,并根据需要显示和隐藏它。这并不难做到,而且您不会将接口推到超出其预期用途的范围。
回答by skywinder
To achieve the same visual effect you can use ActionSheetPicker-3.0, that works with iOS 8.
要实现相同的视觉效果,您可以使用适用于iOS 8 的ActionSheetPicker-3.0。
Actually, it's not UIActionSheet
anymore. But looks exactly the same, and that's why it works on iOS 8.
事实上,已经不是UIActionSheet
了。但看起来完全一样,这就是它适用于iOS 8的原因。
回答by Kirit Vaghela
UIActionSheet is depricated in iOS8. Use UIAlertControllerfor iOS8 or later
UIActionSheet 在iOS8 中被弃用。使用UIAlertController为iOS8上或更高版本
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
回答by folse
I write the time picker by my self instead UIActionSheet in iOS8:
我自己写了时间选择器,而不是 iOS8 中的 UIActionSheet:
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];}
回答by Bulky_ooti
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
回答by Banker Mittal
I have faced the same issue and solved it by this way it will work in all ios version
我遇到了同样的问题并通过这种方式解决了它在所有 ios 版本中都可以使用
In ios 8 apple has changed so many things i here faced one problem in my own project so , i just want to provide the solution in replacement of UIActionsheet
.
在 ios 8 中,苹果改变了很多事情,我在这里在我自己的项目中遇到了一个问题,所以,我只想提供替代UIActionsheet
.
in ios 8 UIActionsheet
deprecated as well its delegate methods also deprecated. Instead of that UIAlertController
with prefferedstyleUIAlertControllerStyleActionSheet
is used. you can check this thread as well:
在 ios 8 中UIActionsheet
已弃用,其委托方法也已弃用。而不是UIAlertController
使用 prefferedstyle UIAlertControllerStyleActionSheet
。你也可以检查这个线程:
https://developer.apple.com/library/ios/documentation/Uikit/reference/UIActionSheet_Class/index.html
https://developer.apple.com/library/ios/documentation/Uikit/reference/UIActionSheet_Class/index.html
This example will work both in ios 8 and previous version of it.
此示例适用于 ios 8 和以前的版本。
in .h file declare this variable
在 .h 文件中声明这个变量
@interface demoProfileForBuyer{
UIActionSheet *ac_sheet_fordate;
UIToolbar *pickerToolBar_fordate;
UIDatePicker *optionPickerDate;
UIAlertController *alertController;
}
in .m file
在 .m 文件中
- (void)viewDidLoad
{
[super viewDidLoad];
pickerToolBar_fordate = [[UIToolbar alloc] initWithFrame:CGRectMake(-8, 0, 320, 44)];
pickerToolBar_fordate.barStyle = UIBarStyleBlack;
[pickerToolBar_fordate sizeToFit];
NSMutableArray *barItemsDate = [[NSMutableArray allkioc] init];
UIBarButtonItem *flexSpaceDate = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItemsDate addObject:flexSpaceDate];
UIBarButtonItem *doneBtnDate = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerViewDoneBirthday)];
[barItemsDate addObject:doneBtnDate];
[pickerToolBar_fordate setItems:barItemsDate animated:YES];
optionPickerDate = [[UIDatePicker alloc] initWithFrame:CGRectMake(-8, 44, 320, 200)];
optionPickerDate.datePickerMode = UIDatePickerModeDate;
[optionPickerDate setMaximumDate: [NSDate date]];
optionPickerDate.datePickerMode = UIDatePickerModeDate;
optionPickerDate.hidden = NO;
optionPickerDate.date = [NSDate date];
optionPickerDate.backgroundColor = [UIColor whiteColor];
if(IS_IOS_8){
alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:alertAction];
[alertController addAction:alertAction];
[alertController addAction:alertAction];
[alertController addAction:alertAction];
[alertController.view addSubview:pickerToolBar_fordate];
[alertController.view addSubview:optionPickerDate];
} else {
ac_sheet_fordate = [[UIActionSheet alloc] initWithTitle:@"test"
delegate:self
cancelButtonTitle:@"cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"dev",@"dev", nil];
[ac_sheet_fordate setActionSheetStyle:UIActionSheetStyleBlackOpaque];
[ac_sheet_fordate setBackgroundColor:[UIColor blackColor]];
[ac_sheet_fordate addSubview:pickerToolBar_fordate];
[ac_sheet_fordate addSubview:optionPickerDate];
}
}
// when date picker open
- (IBAction)showBirthdayPicker:(id)sender{
// self.view.frame = CGRectMake(0, -220, self.view.frame.size.width, self.view.frame.size.height);
UIButton *btnsender = sender;
//[ac_sheet_fordate showInView:self.view];
self.scrForEditProfile.contentSize = CGSizeMake(320, 1000);
if(IS_IOS_8){
popover = alertController.popoverPresentationController;
if (popover)
{
popover.sourceView = sender;
popover.sourceRect = CGRectMake(0, btnsender.bounds.origin.y, btnsender.bounds.size.width, btnsender.bounds.size.height);
popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
}
[self presentViewController:alertController animated:YES completion:nil];
} else {
if(IS_IOS_7){
[ac_sheet_fordate showInView:[UIApplication sharedApplication].keyWindow];
ac_sheet_fordate.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-ac_sheet_fordate.frame.size.height, [UIScreen mainScreen].bounds.size.width, ac_sheet_fordate.frame.size.height);
}
else{
[ac_sheet_fordate showInView:self.view];
}
}
if(IS_IOS_7==FALSE && IS_HEIGHT_GTE_568==FALSE){
[self.scrForEditProfile scrollRectToVisible:CGRectMake(self.txtDOB.frame.origin.x, self.txtDOB.frame.origin.y + 200, self.txtDOB.frame.size.width, self.txtDOB.frame.size.height) animated:NO];
}
else{
[self.scrForEditProfile scrollRectToVisible:CGRectMake(self.txtDOB.frame.origin.x, self.txtDOB.frame.origin.y +300, self.txtDOB.frame.size.width, self.txtDOB.frame.size.height) animated:NO];
}
}
// when date picker open user tap on done button
- (IBAction)pickerViewDoneBirthday{
if(IS_IOS_8){
[self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
} else {
[ac_sheet_fordate dismissWithClickedButtonIndex:0 animated:YES];
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy"];
birthday_date = optionPickerDate.date;
[self.btnShowDatePicker setTitle:[formatter stringFromDate:optionPickerDate.date] forState:UIControlStateNormal];
[self.btnShowDatePicker setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.btnShowDatePicker setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
self.scrForEditProfile.contentSize=CGSizeMake(320 , 700);
[self.scrForEditProfile scrollsToTop];
}
you can check my blog also, http://ioscodesample.blogspot.in/2014/10/ios-8-changes-for-actionsheet.html
你也可以查看我的博客, http://ioscodesample.blogspot.in/2014/10/ios-8-changes-for-actionsheet.html
Thanks.
谢谢。
回答by fish.wang
Look at my code
看我的代码
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
message:@"\n\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];
__weak typeof(self) weakSelf = self;
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确认"style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[weakSelf pickActionSheetFinishAction];
[alertController addAction:cancelAction];
[alertController.view addSubview:self.topicPickView];
[self presentViewController:alertController animated:YES completion:nil];
回答by Amal T S
swift 5
迅捷 5
You can use an UIAlertController
to do this.
您可以使用 anUIAlertController
来执行此操作。
@IBAction func btnActionDate(_ sender: Any) {
let picker = UIPickerView(frame: CGRect(x: 0, y: 50, width: self.view.frame.size.width - 16.0, height: 150))
picker.delegate = self
picker.dataSource = self
// let message = "\n\n\n\n\n\n"
let alert = UIAlertController(title: "Title of Action Sheet", message: "", preferredStyle: UIAlertController.Style.actionSheet)
alert.isModalInPopover = true
picker.reloadAllComponents()
//Add the picker to the alert controller
alert.view.addSubview(picker)
// For setting height of Action sheet
let height:NSLayoutConstraint = NSLayoutConstraint(item: alert.view!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 350)
alert.view.addConstraint(height)
let okAction = UIAlertAction(title: "Done", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
let fetchedObj = picker.selectedRow(inComponent: 0)
})
alert.addAction(okAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
Set the delegates & data source of pickerView like this
像这样设置pickerView的委托和数据源
class MyViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
set the delegate & datasource functions
设置委托和数据源功能
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return arrayMonthData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return arrayMonthData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return self.view.frame.size.width - 16.0
}
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 36.0
}
回答by Thanh V? Tr?n
I have the same problem with you, showing a UIPickerView with UIActionSheet is not working in iOS 8. Because UIActionSheet is deprecated and Apple recommends to use UIAlertController. So we should round to wind.
我和你有同样的问题,在 iOS 8 中显示带有 UIActionSheet 的 UIPickerView 不起作用。因为 UIActionSheet 已被弃用,Apple 建议使用 UIAlertController。所以我们要顺风顺水。
I wrote some code to resolve my case. I hope it can help you.
我写了一些代码来解决我的情况。我希望它能帮助你。
#define CONTENT_HEIGHT 478
- (void)initSheetWithWidth:(CGFloat)aWidth
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"hack space" message:@"\n\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];
[sheet.view setBounds:CGRectMake(7, 0, aWidth, CONTENT_HEIGHT)]; // Kinda hacky
self.sheet = sheet;
} else {
UIActionSheet *sheet = [[UIActionSheet alloc] init];
self.sheet = sheet;
}
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aWidth, CONTENT_HEIGHT)];
[view setBackgroundColor:[UIColor whiteColor]];
UIToolbar *toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, aWidth, 0)];
toolbar.barStyle = UIBarStyleDefault;
[toolbar sizeToFit];
[toolbar setItems:@[
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(pickerSheetCancel)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerSheetDone)]
]];
UIDatePicker *picker = [[UIDatePicker alloc]
initWithFrame:CGRectMake(0, toolbar.bounds.size.height, aWidth, 0)];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
UIAlertController *sheet = self.sheet;
[view addSubview:toolbar];
[view addSubview:picker];
[sheet.view addSubview:view];
} else {
UIActionSheet *sheet = self.sheet;
[sheet addSubview:toolbar];
[sheet addSubview:picker];
}
self.picker = picker;
}
- (void)showWithDate:(NSDate*)date {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
// Reload and select first item
[self.picker setDate:date];
[self.parentViewController presentViewController:self.sheet animated:YES completion:nil];
} else {
UIActionSheet *sheet = self.sheet;
[self.sheet showInView:self.containerView];
// XXX: Kinda hacky, but seems to be the only way to make it display correctly.
[self.sheet
setBounds:CGRectMake(0, 0,
self.containerView.frame.size.width,
sheet.frame.size.height - CONTENT_HEIGHT)];
// Reload and select first item
[self.picker setDate:date];
[UIView animateWithDuration:0.25 animations:^{
[self.sheet
setBounds:CGRectMake(0, 0,
self.containerView.frame.size.width,
sheet.frame.size.height)];
}];
}
}
I think it's all to fix this issue. Happy coding!
我认为这一切都是为了解决这个问题。快乐编码!