您的位置:首页 > 产品设计 > UI/UE

UITextField一些技巧总结

2015-07-01 00:00 543 查看
摘要: UITextField在不同的场景下可能需要不同的需求,我今天总结了使用的一些情况,与大家分享,以后有新的需求,还会更新。。。

我们在项目中肯定要用到UITextField允许用户输入,比如在登录、注册界面。小小的TextField有必要专门出一篇教程吗?当然是有必要的,因为使用场景不同,所以就有了不同的需求,本篇博客结合自己使用的一些需求,写出来与大家分享,如果有什么不对或者不足,请指正;如果大家有更好的方案,希望能够与我分享。另:本篇博客采用xib的编码方式,在关键地方会贴有图片,因为xib可以提高写博客速度,而且这也是ios开发的一个趋势。

1、需求1:改变TextField背景图片,
默认的TextField的Border Style这样的



此时你想设置背景图片,是没有效果的。选择其他三种Border Style,就可以设置背景图片了,很脑残的例子吧。

2、多个TextField时候,点击软键盘的换行(return)按钮,FirstResponder的切换
其实,就是将FirstResponder切换到下一个UITextField,到最后一个TextField时候,点击换行(return)按钮,键盘退出。
步骤:
首先在ErrorViewController.xib上面拖4个UITextField(声明下,这里的ErrorViewController中的Error没有什么“错误的意思”,就是随便命名的,各位不要被误导),从上到下一次摆放,然后右键连线TextField至File’s Owner,这是为了设置delegate,之前的博客我已经说明了设置代理delegate的重要性,这一步往往容易被忽略和遗忘。接着,关联xib中的四个UITextField到ErrorViewController.h,分别命名为errorTF1、errorTF2、errorTF3、errorTF4。
ErrorViewController.h代码如下
@interface ErrorViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *errorTF1;
@property (strong, nonatomic) IBOutlet UITextField *errorTF2;
@property (strong, nonatomic) IBOutlet UITextField *errorTF3;
@property (strong, nonatomic) IBOutlet UITextField *errorTF4;
@end
在ErrorViewController.m中,ErrorViewController实现UITextFieldDelegate协议,然后实现协议中一个optional的方法- (BOOL)textFieldShouldReturn:(UITextField *)textField,ErrorViewController的部分代码如下,
@interface ErrorViewController ()<UITextFieldDelegate>

@end

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return NO;//这里return NO或者YES貌似没有什么区别,我很疑惑,希望高手给我解惑。
}
这样就完成了基本要做的事情,然后是最后一步,就是完善并填充- (BOOL)textFieldShouldReturn:(UITextField *)textField方法,其完整代码如下,
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == errorTF1) {
[errorTF2 becomeFirstResponder];
} else if (textField == errorTF2) {
[errorTF3 becomeFirstResponder];
} else if (textField == errorTF3) {
[errorTF4 becomeFirstResponder];
} else if (textField == errorTF4){
[errorTF4 resignFirstResponder];
}
return NO;//YES or NO,that is a question
}
这段代码相信大家都看得懂,不多做解释了,这样就实现了点击换行(return)按钮,firstResponder跳转到下一个TextField,到最后一个TextField时候,键盘退出。

3、需求2:多个TextField,键盘遮挡,界面上移,
本来准备写一下错误的代码,给各位一点提醒不要误入歧途,但是忘了之前错误代码怎么写的,而且再重写的话也比较浪费时间,所以直接写出我整理好的正确的方法吧。我们知道,在iPhone竖屏的情况下,软键盘的高度是216,所以如果为了不让键盘挡住TextField,那么应该把界面向上移动,让TextField的底部刚好在键盘的顶部,这种情况下使用NSNotification通知,是能够比较好的解决这个问题。
这次拖动10个UITextField到TFViewController.xib里面,重复上面“需求2”的步骤,设置delegate,在TFViewController.h中声明10个UITextField property,分别命名为tf1、tf2、tf3、……、tf10,TFViewController实现UITextFieldDelegate协议,并实现其中方法-(BOOL)textFieldShouldReturn:(UITextField *)textField可选(optional)方法,如下所示,
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == tf1) {
[tf2 becomeFirstResponder];
} else if (textField == tf2)
{
[tf3 becomeFirstResponder];
} else if (textField == tf3)
{
[tf4 becomeFirstResponder];
} else if (textField == tf4)
{
[tf5 becomeFirstResponder];
} else if (textField == tf5)
{
[tf6 becomeFirstResponder];
} else if (textField == tf6)
{
[tf7 becomeFirstResponder];
} else if (textField == tf7)
{
[tf8 becomeFirstResponder];
} else if (textField == tf8)
{
[tf9 becomeFirstResponder];
} else if (textField == tf9) {
[tf10 becomeFirstResponder];
} else if (textField == tf10) {
[tf10 resignFirstResponder];
}
return YES;
}
这几个步骤和“需求2”没有区别,之所以重复一遍,是因为这几个步骤是“需求3”实现的基础,所以写一遍也不嫌多。
接下来就是实现界面上移,不遮挡TextField的关键地方了。很幸运的是,苹果在系统给我们封装了“键盘弹出”和“键盘消失”的事件,此处我们只需要用到“键盘消失”的系统通知,我们可以使用通知来监听键盘的行为,判断当前为firstResponder的TextField是否被键盘遮挡,如果被遮挡,那么屏幕上移,否则不做处理,这是很简单的逻辑,实现的代码如下,

(2)在TFViewController的- (void)viewWillAppear里面添加监听“键盘消失”的通知,
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];

}
相应的,我们需要在- (void)viewWillDisappear时候,将NSNotification移除,这就像内存管理一样,你在不用的时候需要移除你添加的通知,
键盘弹出和消失的系统事件就是UIKeyboardWillShowNotification和UIKeyboardWillHideNotification,针对“键盘消失”的情况keyboardWillHide来处理,当然这些代码是写在TFViewController.m文件中的,下面就是实现这该方法的代码,
- (void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}];

}
这是将self.view移回到原来位置的方法,那么对应的将界面上移的代码是写在什么地方的呢?就是写在某个TextField变为firstResponder、处于被编辑的时候,我们进行判断并看是否要上移self,view,代码如下,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.3 animations:^(void) {
CGFloat keyboardHeight = 216;//键盘高度
//textField底部的坐标
CGFloat bottomY = textField.frame.origin.y+textField.frame.size.height;
//VIEW底部到textField底部的距离
CGFloat tempHeight = self.view.frame.size.height-bottomY;
//键盘高度超过BUtton底部的距离,即需要view的frame上提得高度
CGFloat upOffset = keyboardHeight-tempHeight;
if (upOffset>0) {
[UIView animateWithDuration:0.3 animations:^{
self.view.frame = CGRectMake(0, -upOffset, self.view.frame.size.width,self.view.frame.size.height);
}];
}
}];
}
这样键盘就不会挡住TextField,不会让用户不能输入的。
—————————————————————————————————————————————
各位有没有觉得这样实现,代码写的不“对称”,因为UIKeyboardWillHideNotification和UIKeyboardWillShowNotification一起出现才感觉好,还有就是代理方法- (void)textFieldDidBeginEditing和- (void)textFieldDidEndEditing一起出现才完美,实际情况是我也想这样才好,但是没用到就没用到了呗,有强迫症的观众不要抓狂啊。其实我也试过了,如果只使用UIKeyboardWillHideNotification和UIKeyboardWillShowNotification不能实现想要的结果,如果只使用textFieldDidEndEditing和textFieldDidBeginEditing同样一不行,我这样交叉着各使用了一个,恰好实现了。如果有人能写出“对称”的代码,来实现这个功能,请让我也知道。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  TextField