English 中文(简体)
将UIDatePicker适合UIActionSheet中。
原标题:
  • 时间:2008-12-08 15:27:08
  •  标签:

我正在尝试将一个带有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的位置/大小。

最佳回答

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

    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 ->选择器示例。

问题回答

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

[menu sendSubviewToBack:pickerView];

Dmitry的答案几乎正确。但是,操作表的框架太小,因此底部附近的触摸会被忽略。我已经纠正了这些问题:

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

对我来说这行得通

NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"








" : @"











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

有点棘手,但它有效!

按钮在顶部,选择器在底部:

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

确保在UITabBarController内部正确显示UIActionSheet,以避免底部交互问题。

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

可能没有直接回答特定问题(如何使UIDatePicker适合),但对于呈现UIDatePicker(还有其他内容)的UIActionSheet实现,请参见ActionSheetPicker 。从它的自述文件中:

轻松使用PickerView展示ActionSheet,让用户从不可变选项中进行选择。基于在mobilesafari中找到的HTML下拉选项的替代方案。

同样:

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

即便你不使用这个代码,你也应该在自己动手之前好好阅读它。


Update:

此版本已过时:请使用ActionSheetPicker-3.0





相关问题
热门标签