Cocoa touch 如何以方向友好的方式在选项卡栏上显示UIDatePicker?

Cocoa touch 如何以方向友好的方式在选项卡栏上显示UIDatePicker?,cocoa-touch,orientation,hide,uitabbar,uidatepicker,Cocoa Touch,Orientation,Hide,Uitabbar,Uidatepicker,当我的用户点击我的表视图日期字段时,我想滑入一个UIDatePicker,就像在标准联系人应用程序中,当用户点击生日字段时一样。有一个额外的杀手细节: 它位于选项卡栏应用程序中,我希望UIDatePicker在选项卡栏上滑动。当用户将手机置于lanscape方向时,手机仍会旋转 我显示UIDatePicker的方式是在视图中插入,然后设置其位置的动画: [self.view addSubview: self.pickerView]; 执行此操作时,会显示UIDatePicker

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

它位于选项卡栏应用程序中,我希望
UIDatePicker
在选项卡栏上滑动。当用户将手机置于lanscape方向时,手机仍会旋转

我显示
UIDatePicker
的方式是在视图中插入,然后设置其位置的动画:

        [self.view addSubview: self.pickerView];
执行此操作时,会显示
UIDatePicker
,但不会覆盖选项卡栏

我还可以将其添加到窗口:

        [self.view.window addSubview: self.pickerView];
UIDatePicker
可以正确地在选项卡栏上滑动,但是它不遵循方向

我发现的唯一一种半可接受的方法是使用

        detailViewController.hidesBottomBarWhenPushed = YES;
但这不是我想要的:我想要细节视图中的选项卡栏。我只想在选日期的时候把它拿走。我不能将
UIDatePicker
放在它自己的控制器中,该控制器具有
hidesBottomBarWhenPushed=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;
    }

}

您应该将
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:shouldChangeCharactersRange:replacementString:
中返回
NO
,以禁用外部键盘输入。但是现在,我还想禁用
UITextField
中闪烁的文本光标。你知道怎么做吗?禁用
UIEditField
太强:不再显示日期选择器视图。谢谢。如果您在执行此操作时遇到任何问题,请告诉我。