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)];

这会产生正确定位的警报表(忽略 nil 代表;这仅用于显示目的)。问题出在这里:

替代文字
(来源:hosting-wizards.com

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

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

旁注:上面粘贴的代码没有将UIDatePicker定位在图片中的相同位置,但是无论UIDatePicker放置在哪里都会出现分层问题。分层问题只在顶部,不在底部。

4

5 回答 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 中粘贴日期选择器看起来非常俗气。

于 2010-01-08T09:48:32.207 回答
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 要好得多。您仍然需要在“完成”按钮上添加自己的操作处理程序,并且当用户更改选择器时,但这应该可以帮助您入门。

于 2011-09-12T21:19:54.933 回答
1

尝试这个

[menu sendSubviewToBack: pickerView];

并将您的选择器向下移动 10 像素,正如您在屏幕截图中看到的那样,它没有与底部对齐,可能是

[pickerView setFrame:CGRectMake(0, 100, 320, 216)];
于 2011-01-06T05:04:30.847 回答
1

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

于 2010-10-01T09:42:49.187 回答
0

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

于 2010-01-06T21:49:14.563 回答