将UIDatePicker装入UIActionSheet
我试图得到UIDatePicker与UIButton显示在UIActionSheet。 不幸的是,它被裁剪掉,整个date选取器不可见。 我甚至没有尝试添加UIButton。 任何人都可以build议在整个视图适合正确? 我不知道如何添加适当的尺寸,因为UIActionSheet似乎缺less一个-initWithFrame:
types的构造函数。
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和导航栏创build全屏视图。 有关示例,请参阅iPhone DevCenter中的UICatalog – > Pickers样本。
尝试添加以下行来解决禁用的button问题
[menu sendSubviewToBack:pickerView];
德米特里的回答几乎是正确的。 但是,操作单的框架太小,因此触摸select器的底部附近被忽略。 我纠正了这些问题:
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) ? @"\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];
有点棘手,但它的作品!
顶部button,底部选取器:
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,允许用户从一些不可变的选项中进行select。 基于mobilesafari中的HTML下拉选项。
也:
一些代码来自Stack Overflow上的marcio的post [ 添加UIPickerView和一个button在操作表 – 如何? ]
即使不使用代码,在阅读自己的代码之前也是很好的阅读。
更新:
此版本已弃用:改为使用ActionSheetPicker-3.0 。