37

我试图让带有 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];

我也尝试过类似的东西:

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

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

4

7 回答 7

43

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

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

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

于 2008-12-09T18:42:21.700 回答
18

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

[menu sendSubviewToBack:pickerView];
于 2009-10-07T08:23:40.647 回答
17

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

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];  
于 2010-09-28T17:19:14.557 回答
8

这对我有用

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

有点棘手,但它的工作原理!

于 2010-04-20T15:52:57.687 回答
7

顶部按钮,底部选择器:

 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];  
于 2010-07-06T11:57:58.193 回答
4

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

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

于 2010-08-10T10:04:23.690 回答
3

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

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

还:

一些代码源自 Marcio 在 Stack Overflow 上的帖子 [在操作表中添加 UIPickerView 和按钮 - 怎么样?]

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


更新:

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

于 2011-10-04T12:04:38.817 回答