objective-c 将 UIDatePicker 装入 UIActionSheet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/349858/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:58:24  来源:igfitidea点击:

Fitting a UIDatePicker into a UIActionSheet

objective-ciphonecocoa-touchuidatepicker

提问by Coocoo4Cocoa

I'm trying to get a UIDatePicker with a UIButton to show up in a UIActionSheet. Unfortunately it gets cropped off and the entire Date Picker is not visible. I have not even attempted to add the UIButton yet. Can anyone suggest on getting the entire view to fit properly? I'm not sure how to add the proper dimensions as UIActionSheet seems to lack an -initWithFrame:type constructor.

我试图让一个带有 UIButton 的 UIDatePicker 显示在 UIActionSheet 中。不幸的是,它被裁剪掉了,整个日期选择器不可见。我什至还没有尝试添加 UIButton。任何人都可以建议让整个视图正确适合吗?我不确定如何添加适当的维度,因为 UIActionSheet 似乎缺少-initWithFrame:类型构造函数。

UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" 
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel"
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:nil];

// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDate;
[menu addSubview:pickerView];
[menu showInView:self.view];

[pickerView release];
[menu release];

I've also tried with something similar to:

我也尝试过类似的东西:

UIActionSheet *menu = [[UIActionSheet alloc] initWithFrame:CGRectMake(200.0, 200.0, 100.0f, 100.0f)];

The coords are ofcourse not realistic, but they don't seem to affect the position/size of the UIActionSheet.

坐标当然不现实,但它们似乎不会影响 UIActionSheet 的位置/大小。

回答by thbonk

You can use something like this (adjust the coordinates):

你可以使用这样的东西(调整坐标):

    UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" 
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel"
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:nil];

    // Add the picker
    UIDatePicker *pickerView = [[UIDatePicker alloc] init];
    pickerView.datePickerMode = UIDatePickerModeDate;
    [menu addSubview:pickerView];
    [menu showInView:self.view];    
    [menu setBounds:CGRectMake(0,0,320, 500)];

    CGRect pickerRect = pickerView.bounds;
    pickerRect.origin.y = -100;
    pickerView.bounds = pickerRect;

    [pickerView release];
    [menu release];

But you better create a fullscreen view with a UIDatePicker and a navigation bar. For an example see UICatalog -> Pickers sample from the iPhone DevCenter.

但是你最好用 UIDatePicker 和导航栏创建一个全屏视图。有关示例,请参阅 iPhone DevCenter 中的 UICatalog -> Pickers 示例。

回答by Ajay Sawant

Try adding following line to resolve disabled buttons issue

尝试添加以下行来解决禁用按钮问题

[menu sendSubviewToBack:pickerView];

回答by Jakob Egger

Dmitry's answer is almostcorrect. However, the frame of the actionsheet is too small, and therefore touches near the bottom of the picker are ignored. I have corrected these problems:

德米特里的回答几乎是正确的。但是,操作表的框架太小,因此忽略选择器底部附近的触摸。我已经纠正了这些问题:

UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" 
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel"
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:@"OK",nil];    
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeTime;
[menu addSubview:pickerView];
[menu showInView:_mainView];        

CGRect menuRect = menu.frame;
CGFloat orgHeight = menuRect.size.height;
menuRect.origin.y -= 214; //height of picker
menuRect.size.height = orgHeight+214;
menu.frame = menuRect;


CGRect pickerRect = pickerView.frame;
pickerRect.origin.y = orgHeight;
pickerView.frame = pickerRect;

[pickerView release];
[menu release];  

回答by Oscar Peli

This works for me

这对我有用

NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ;
UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                              initWithTitle:[NSString stringWithFormat:@"%@%@", title, NSLocalizedString(@"SelectADateKey", @"")]
                              delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Ok", nil];
[actionSheet showInView:self.view];
UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
[actionSheet addSubview:datePicker];

a little tricky but it works!

有点棘手,但它有效!

回答by Dmitry

Buttons at top, picker at bottom:

顶部按钮,底部选择器:

 UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" 
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel"
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:@"OK",nil];    
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeTime;
[menu addSubview:pickerView];
[menu showInView:_mainView];        

CGRect menuRect = menu.frame;
menuRect.origin.y -= 214;
menuRect.size.height = 300;
menu.frame = menuRect;


CGRect pickerRect = pickerView.frame;
pickerRect.origin.y = 174;
pickerView.frame = pickerRect;

[pickerView release];
[menu release];  

回答by Stefan

Make sure you show the UIActionSheet in the right view when inside a UITabBarController to avoid interactivity problems at the bottom:

确保在 UITabBarController 内部时在右侧视图中显示 UIActionSheet 以避免底部的交互问题:

[actionSheet showInView:self.parentViewController.tabBarController.view];

[actionSheet showInView:self.parentViewController.tabBarController.view];

回答by mts

Possibly not directly answering the specific question (how to get the UIDatePicker to fit), but for an implementation of UIActionSheet which presents a UIDatePicker (also others) please see ActionSheetPicker. From it's Readme:

可能不是直接回答特定问题(如何让 UIDatePicker 适合),但对于呈现 UIDatePicker(还有其他人)的 UIActionSheet 的实现,请参阅ActionSheetPicker。从它的自述文件:

Easily present an ActionSheet with a PickerView, allowing user to select from a number of immutable options. Based on the HTML drop-down alternative found in mobilesafari.

使用 PickerView 轻松呈现 ActionSheet,允许用户从许多不可变选项中进行选择。基于在 mobilesafari 中找到的 HTML 下拉选项。

Also:

还:

Some code derived from marcio's post on Stack Overflow[ Add UIPickerView & a Button in Action sheet - How?]

一些代码源自 Marcio 在 Stack Overflow 上的帖子[ Add UIPickerView & a Button in Action sheet - How?]

Even if one doesn't use the code, it's good reading before you roll your own.

即使一个人不使用代码,在你自己动手之前阅读也是很好的。



Update:

更新:

This version is deprecated: use ActionSheetPicker-3.0instead.

此版本已弃用:请改用ActionSheetPicker-3.0