UIAatePicker在UIActionSheet分层问题中

问题描述 投票:6回答:5

有人在另一个问题中发布了这个代码,将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委托;这仅用于显示目的)。问题在于:

alt text (来源:hosting-wizards.com

如您所见,在分钟滚动条上方,存在分层问题。我还没有确认是否在设备上发生这种情况。关于为什么会发生这种情况的任何想法?

另一个问题:为什么UIActionSheet上的setBounds选择器将Y-size设置为如此高的值以便正确显示?如果不是,将Y尺寸设置为480应创建全屏显示?将此值设置为零会使其几乎不可见。

侧面注意:上面粘贴的代码不会将UIDatePicker放置在图片中的相同位置,但无论UIDatePicker放在何处,都会出现分层问题。分层问题仅在顶部,而不在底部。

iphone objective-c uialertview uidatepicker
5个回答
8
投票

如果您只是将UIDatePicker放在新视图中并自己实现从下滑动的行为,您将避免显示问题并获得更好看的结果。这并不难。

  1. 创建一个包含日期选择器的UIView和一个带有Done按钮的工具栏。 (在代码中或使用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要好得多,ActionSheets旨在显示按钮,而不是选择器。你仍然需要在“完成”按钮上添加自己的动作处理程序,当用户更改选择器时,这应该让你开始。


1
投票

你真的需要把你的选择器放在UIActionSheet上吗?为什么不使用UIResponder类的inputView属性?当您的控件(例如UITextField)成为第一响应者时,它会自动显示UIDatePicker(或任何其他视图)。可以在此处找到使用带有源代码的inputView的示例:Working with pickers


1
投票

试试这个

[menu sendSubviewToBack: pickerView];

并且如你在屏幕截图中看到的那样,将你的选择器向下移动了10个像素,它可能不是底部

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

0
投票

您可能会发现此主题有助于替代方法:http://discussions.apple.com/message.jspa?messageID=7923095

© www.soinside.com 2019 - 2024. All rights reserved.