有人在另一个问题中发布了这段代码,将 UIDatePicker 放入 UIAlertSheet 中:
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];
我修改成这样:
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:@"Done"
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeCountDownTimer;
[menu addSubview:pickerView];
[menu showInView:self.navigationController.tabBarController.view];
[menu setBounds:CGRectMake(0,0,320, 516)];
[pickerView setFrame:CGRectMake(0, 85, 320, 216)];
这会产生一个正确定位的警告表(忽略 nil 委托(delegate);这仅用于显示目的)。问题出在这里:
(来源:hosting-wizards.com)
如您所见,在分钟滚动条上方,存在分层问题。我还没有确认这是否发生在设备上。关于为什么会发生这种情况的任何想法?
另一个问题:为什么 UIActionSheet 上的 setBounds 选择器将 Y 大小设置为如此高的值才能正确显示?将 Y 大小设置为 480 应该会创建全屏显示,不是吗?将此值设置为零可使其几乎不可见。
SIDE NOTE: 上面粘贴的代码没有将 UIDatePicker 放置在图片中的同一位置,但是无论 UIDatePicker 放在哪里,都会出现分层问题。分层问题只在顶部,不在底部。
最佳答案
如果您只是将 UIDatePicker 放在新 View 中并自己实现从底部向上滑动的行为,您将避免显示问题并获得更好看的结果。没那么难。
- 创建一个包含日期选择器和带有完成按钮的工具栏的 UIView。 (在代码中或使用 Interface Builder,都没有关系。)
- 将弹出 View 添加为主视图的 subview 。
- 设置弹出 View 的框架,使其远离屏幕底部:
frame.origin.y = CGRectGetMaxY(mainView.bounds)
- 调用
[UIView beginAnimations:nil context:nil]
- 将框架设置到它应该在的位置:
frame.origin.y -= CGRectGetHeight(popupView.bounds)
- 调用
[UIView commitAnimations]
(请注意,框架设置内容是伪代码。)
就是这样。当您需要关闭 View 时,请反向执行此操作。我认为这是值得的;在 UIActionSheet 中粘贴日期选择器看起来非常俗气。
关于iphone - UIActionSheet 中的 UIDatePicker 分层问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1191569/