UIDatePicker在UIActionSheet中的层叠问题

6

有人在另一个问题中发布了以下代码,将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)];

这将产生一个正确定位的警报表(忽略空委托;这只是为了显示目的)。问题在于这里:

alt text
(来源:hosting-wizards.com

正如您所看到的,在分钟滚动器上方存在分层问题。我尚未确认此是否发生在设备上。有什么想法,为什么会发生这种情况?

另一个问题:为什么UIActionSheet上的setBounds选择器具有如此高的Y大小值以进行正确显示?将Y大小设置为480应该创建全屏显示,不是吗?将此值设置为零几乎使其不可见。

顺便说一下:上面粘贴的代码并没有将UIDatePicker定位在图片中的同一位置,但无论UIDatePicker放置在哪里,分层问题都会发生。分层问题只出现在顶部而不是底部。

5个回答

8
如果您将UIDatePicker放在一个新的视图中,并自己实现从底部滑动上来的行为,那么您将避免显示问题并获得更好的外观效果。这并不难。
以下是步骤:
1. 创建一个包含日期选择器和带有完成按钮的工具栏的UIView。(可以使用代码或Interface Builder)
2. 将弹出视图添加为主视图的子视图。
3. 设置弹出视图的框架,使其位于屏幕底部之外:frame.origin.y = CGRectGetMaxY(mainView.bounds) 4. 调用[UIView beginAnimations:nil context:nil] 5. 将框架设置为它应该在的位置:frame.origin.y -= CGRectGetHeight(popupView.bounds) 6. 调用[UIView commitAnimations] 注意:框架设置部分是伪代码。
这样做反向操作以关闭视图。我认为这会值得努力;把日期选择器放在UIActionSheet中看起来非常俗气。

3

使用benzado上面的步骤,如果您使用UIView而不是ActionSheet,您的代码大致如下:

int height = 255;

//create new view
UIView * newView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 320, height)];
newView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];

//add toolbar
UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 40)];
toolbar.barStyle = UIBarStyleBlack;

//add button
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:nil];
toolbar.items = [NSArray arrayWithObjects:infoButtonItem, nil];

//add date picker
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
datePicker.frame = CGRectMake(0, 40, 320, 250);
[datePicker addTarget:self action:nil/*@selector(changeDateInLabel:)*/ forControlEvents:UIControlEventValueChanged];
[newView addSubview:datePicker];

//add popup view
[newView addSubview:toolbar];
[self.view addSubview:newView];

//animate it onto the screen
CGRect temp = newView.frame;
temp.origin.y = CGRectGetMaxY(self.view.bounds);
newView.frame = temp;
[UIView beginAnimations:nil context:nil];
temp.origin.y -= height;
newView.frame = temp;
[UIView commitAnimations];

我同意,这比使用ActionSheets要好得多,因为它们是用来显示按钮而不是选择器的。您仍然需要在“完成”按钮上添加自己的操作处理程序以及当用户更改选择器时,但这应该可以让您开始了。


1

试试这个

[menu sendSubviewToBack: pickerView];

同时,根据屏幕截图所示,将您的选取器向下移动10像素,以使其与底部对齐。

[pickerView setFrame:CGRectMake(0, 100, 320, 216)];

1
你真的需要把你的选择器放在UIActionSheet上吗?为什么不使用UIResponder类的inputView属性呢?当你的控件(如UITextField)成为第一响应者时,它会自动显示UIDatePicker(或任何其他视图)。 这里有一个使用源代码的inputView示例:使用选择器

inputView在iOS 4.0及更高版本中可用。如果要支持3.1,就不能使用它。 - o_o

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接