【问题标题】:Elegantly replace iPhone keyboard with UIPickerView用 UIPickerView 优雅地替换 iPhone 键盘
【发布时间】:2010-11-19 12:35:36
【问题描述】:

我有一个嵌入 UITextFields 的表格视图,用于输入一些数据。它还有两个弹出 UIPickerView 和 UIDatePicker 的字段 - 如 Apple 的 DateCell 示例所示。

大多数情况下它可以工作,但我不知道如何从文本字段键盘干净地过渡到其他选择器 - 一个滑出,一个滑入 - 看起来很奇怪,表格视图上的滚动位置有时会搞砸- 所有内容都从顶部滚动。

我想要做的是替换文本字段键盘而不让它动画消失,也不会将表格视图恢复到完整大小。

有什么线索吗?

【问题讨论】:

    标签: iphone uitextfield uipickerview uidatepicker


    【解决方案1】:

    首先,这是screencapture showing how this looks

    实现 UITextFieldDelegate 并显示一个包含 UIPickerView 的“弹出窗口”。

    - (void)textFieldDidEndEditing:(UITextField *)textField {
    
        UIPickerView *picker = [[UIPickerView alloc] 
                                     initWithFrame:CGRectMake(0, 244, 320, 270)];
        picker.delegate = self;
        picker.dataSource = self;
        [self.view addSubview:picker];
        [picker release];
    }
    

    当键盘消失时,选择器视图随即可见。

    如果您想更进一步,您可以像键盘一样为 UIPickerView“滑入”设置动画。

    - (void)viewDidLoad {
    
        //picker exists in the view, but is outside visible range
        picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 480, 320, 270)];
        picker.delegate = self;
        picker.dataSource = self;
        [self.view addSubview:picker];
        [picker release];
    }
    
    //animate the picker into view
    - (void)textFieldDidEndEditing:(UITextField *)textField {
    
        [UIView beginAnimations:@"picker" context:nil];
        [UIView setAnimationDuration:0.5];
    
        picker.transform = CGAffineTransformMakeTranslation(0,-236);
        [UIView commitAnimations];
    
    }
    
    //animate the picker out of view
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
    
        [UIView beginAnimations:@"picker" context:nil];
        [UIView setAnimationDuration:0.5];
    
        picker.transform = CGAffineTransformMakeTranslation(0,236);
        [UIView commitAnimations];
    }
    
    //just hide the keyboard in this example
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [textField resignFirstResponder];
        return NO;
    }
    

    【讨论】:

    • 请注意,这是执行此操作的旧方法。更“现代”的版本将使用 textField 的 inputView 属性。
    【解决方案2】:

    只需将 UITextField 的 inputView 属性设置为 UIPickerView。

    【讨论】:

    猜你喜欢
    • 2016-08-07
    • 2015-07-22
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    相关资源
    最近更新 更多