我正在尝试使用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 - > Pickers示例.
尝试添加以下行以解决已禁用的按钮问题
[menu sendSubviewToBack:pickerView];
德米特里的回答几乎是正确的.但是,动作表的框架太小,因此忽略了拾取器底部附近的触摸.我纠正了这些问题:
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];
有点棘手,但它的工作原理!
顶部的按钮,底部的选择器:
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];