【问题标题】:How to show a UIDatePicker over the tab bar, in an orientation-friendly way?如何以方向友好的方式在标签栏上显示 UIDatePicker?
【发布时间】:2011-09-16 01:51:53
【问题描述】:

我想在用户点击我的表格视图日期字段时滑入UIDatePicker,就像在标准联系人应用程序中一样,当用户点击生日字段时。加上一个额外的杀手级细节:

它在标签栏应用程序中,我希望UIDatePicker 滑过标签栏。当用户将手机横向放置时,它仍然会旋转。

我显示UIDatePicker的方式是在视图中插入,然后对其位置进行动画处理:

        [self.view addSubview: self.pickerView];

当我这样做时,UIDatePicker 会显示,但不会覆盖标签栏。

我也可以将它添加到窗口中:

        [self.view.window addSubview: self.pickerView];

UIDatePicker 正确地滑过标签栏,但是,它不遵循方向。

我发现的唯一半可接受的方法是完全不使用标签栏,使用

        detailViewController.hidesBottomBarWhenPushed = YES;

但这不是我想要的:我想要详细视图中的标签栏。我只希望它在选择日期时消失。我不能将UIDatePickerhidesBottomBarWhenPushed = YES 一起放在它自己的控制器中,因为这会完全覆盖屏幕,我宁愿让细节视图仍然部分可见。

欢迎提出任何建议。

这是我用来显示UIDatePicker 的完整代码,从一些示例代码中复制而来:

- (void) doPickDate
{
    NSDate *initialDateForPicker = [(ExpenseData*) self.displayedObject displayDate];
    self.pickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    self.pickerView.datePickerMode = UIDatePickerModeDate;
    self.pickerView.date = initialDateForPicker;

    // check if our date picker is already on screen
    if (self.pickerView.superview == nil)
    {
        [self.view addSubview: self.pickerView];

        // size up the picker view to our screen and compute the start/end frame origin for our slide up animation
        // compute the start frame
        CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
        CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
        CGRect startRect = CGRectMake(0.0,
                                      screenRect.origin.y + screenRect.size.height,
                                      pickerSize.width, pickerSize.height);
        self.pickerView.frame = startRect;

        // compute the end frame
        CGRect pickerRect = CGRectMake(0.0,
                                       screenRect.origin.y + screenRect.size.height - pickerSize.height,
                                       pickerSize.width,
                                       pickerSize.height);
        // start the slide up animation
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];

        // we need to perform some post operations after the animation is complete
        [UIView setAnimationDelegate:self];

        self.pickerView.frame = pickerRect;

        // shrink the table vertical size to make room for the date picker
        CGRect newFrame = self.tableView.frame;
        newFrame.size.height -= self.pickerView.frame.size.height - 49 /* tab bar height */;
        self.tableView.frame = newFrame;
        [UIView commitAnimations];

        // add the "Done" button to the nav bar
        self.navigationItem.rightBarButtonItem = self.doneButton;
    }

}

【问题讨论】:

    标签: cocoa-touch orientation hide uitabbar uidatepicker


    【解决方案1】:

    您应该将UIDatePicker 对象设置为该特定文本字段的inputView。所有的动画和演示内容都将得到处理。

    隐藏光标

    没有隐藏光标的方法。我能想到的唯一机制是使用leftView 属性并将其设置为标签。

    CGRect frame = self.textField.bounds;
    
    UILabel * theLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
    theLabel.userInteractionEnabled = YES;
    theLabel.backgroundColor = [UIColor clearColor];
    
    UITapGestureRecognizer * tap = [[[UITapGestureRecognizer alloc] initWithTarget:self
                                                                            action:@selector(tap:)] autorelease];
    [theLabel addGestureRecognizer:tap];
    
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    self.textField.leftView = theLabel;
    self.textField.clipsToBounds = YES;
    

    并使用来处理水龙头,

    - (void)tap:(UIGestureRecognizer *)gesture {
        [self.textField becomeFirstResponder];
    }
    

    现在这不适用于圆角矩形按钮,因为无论标签的框架是什么,光标都会在右上角闪烁。它隐藏了其他边框样式的光标。您还可以通过将值设置为其文本来利用标签。

    UILabel * theLabel = textField.leftView;
    theLabel.text = @"Appropriate Value from Picker";
    

    【讨论】:

    • 好的,这几乎可行。首先,我必须更改我的表格视图单元格以包含 UITextField,否则 UILabel 的 inputView 属性是只读的。我还必须在textField:shouldChangeCharactersInRange:replacementString: 中返回NO 以禁用外部键盘输入。但是现在,我还想禁用UITextField 中的闪烁文本光标。知道怎么做吗?禁用UIEditField 太强了:日期选择器视图不再显示。谢谢。
    • 如果您在执行此操作时遇到任何问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2017-08-14
    • 2023-03-22
    • 2012-02-17
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    相关资源
    最近更新 更多