用UIPickerView优雅地替换iPhone键盘 [英] Elegantly replace iPhone keyboard with UIPickerView

查看:111
本文介绍了用UIPickerView优雅地替换iPhone键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a table view that has embedded UITextFields for entering some data. It also has two other fields that popup a UIPickerView and a UIDatePicker - as demonstrated in the DateCell example from Apple.

大多数情况下它有效但我无法弄清楚如何干净地从文本字段键盘转换到其他拾取器 - 一个滑出,一个滑入 - 它看起来很奇怪,桌面视图上的滚动位置有时会搞砸 - 所有内容都滚动到顶部。

Mostly it works but I can't figure out how to cleanly transition from the text field keyboard to the other pickers - one slides out, one slides in - it looks weird and the scroll position on the table view sometimes gets screwed up - with everything scrolled off the top.

我想要做的就是更换文本字段键盘,不要将其设置为动画,也不要将表视图恢复到完整大小。

What I'd like to do is replace the text field keyboard without it animating away and without it growing the table view back to full size.

任何线索?

推荐答案

首先,这是 screencapture显示它的外观

实现UITextFieldDelegate并显示包含UIPickerView的弹出窗口。

Implement UITextFieldDelegate and display a "popup" containing a 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];
}

当键盘消失时,可以看到选择器视图。

When the keyboard disappears, a picker view is then visible.

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

If you want to take this a bit further, you can animate the UIPickerView "slide in" like the keyboard.

- (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;
}

这篇关于用UIPickerView优雅地替换iPhone键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆