【问题标题】:UIDatePicker in UIActionSheet layering problemUIActionSheet中的UIDatePicker分层问题
【发布时间】:2009-07-28 01:55:47
【问题描述】:

有人在另一个问题中发布了此代码以将 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放置在哪里都会出现分层问题。分层问题只在顶部,不在底部。

【问题讨论】:

    标签: iphone objective-c uialertview uidatepicker


    【解决方案1】:

    如果您简单地将 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 中粘贴日期选择器看起来非常俗气。

    【讨论】:

      【解决方案2】:

      使用上面的 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 要好得多。您仍然需要在“完成”按钮以及用户更改选择器时添加自己的操作处理程序,但这应该可以帮助您开始。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • inputView 在 iOS 4.0 及更高版本中可用。想支持3.1就不能用了
        【解决方案4】:

        试试这个

        [menu sendSubviewToBack: pickerView];
        

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

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 2011-08-21
            • 2010-09-25
            • 2023-03-05
            • 2011-05-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-12-19
            • 2012-08-19
            相关资源
            最近更新 更多