咱们在项目中确定要用到UITextField容许用户输入,好比在登陆、注册界面。小小的TextField有必要专门出一篇教程吗?固然是有必要的,由于使用场景不一样,因此就有了不一样的需求,本篇博客结合本身使用的一些需求,写出来与你们分享,若是有什么不对或者不足,请指正;若是你们有更好的方案,但愿可以与我分享。另:本篇博客采用xib的编码方式,在关键地方会贴有图片,由于xib能够提升写博客速度,并且这也是ios开发的一个趋势。
ios
一、需求1:改变TextField背景图片,app
默认的TextField的Border Style这样的ide
此时你想设置背景图片,是没有效果的。选择其余三种Border Style,就能够设置背景图片了,很脑残的例子吧。学习
二、多个TextField时候,点击软键盘的换行(return)按钮,FirstResponder的切换编码
其实,就是将FirstResponder切换到下一个UITextField,到最后一个TextField时候,点击换行(return)按钮,键盘退出。atom
步骤:spa
首先在ErrorViewController.xib上面拖4个UITextField(声明下,这里的ErrorViewController中的Error没有什么“错误的意思”,就是随便命名的,各位不要被误导),从上到下一次摆放,而后右键连线TextField至File’s Owner,这是为了设置delegate,以前的博客我已经说明了设置代理delegate的重要性,这一步每每容易被忽略和遗忘。接着,关联xib中的四个UITextField到ErrorViewController.h,分别命名为errorTF一、errorTF二、errorTF三、errorTF4。.net
ErrorViewController.h代码以下3d
@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;
在ErrorViewController.m中,ErrorViewController实现UITextFieldDelegate协议,而后实现协议中一个optional的方法- (BOOL)textFieldShouldReturn:(UITextField *)textField,ErrorViewController的部分代码以下,
@interface ErrorViewController ()<UITextFieldDelegate>
- (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时候,键盘退出。
三、需求2:多个TextField,键盘遮挡,界面上移,
原本准备写一下错误的代码,给各位一点提醒不要误入歧途,可是忘了以前错误代码怎么写的,并且再重写的话也比较浪费时间,因此直接写出我整理好的正确的方法吧。咱们知道,在iPhone竖屏的状况下,软键盘的高度是216,因此若是为了避免让键盘挡住TextField,那么应该把界面向上移动,让TextField的底部恰好在键盘的顶部,这种状况下使用NSNotification通知,是可以比较好的解决这个问题。
此次拖动10个UITextField到TFViewController.xib里面,重复上面“需求2”的步骤,设置delegate,在TFViewController.h中声明10个UITextField property,分别命名为tf一、tf二、tf三、……、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一样一不行,我这样交叉着各使用了一个,刚好实现了。若是有人能写出“对称”的代码,来实现这个功能,请让我也知道。
本人建了一个ios交流群188647173,但愿你们能在群里相互学习,共同进步。求大神照耀啊。。。
个人邮箱是zheniyerenrou@163.com,有什么质疑或者疑问,能够发我邮件。