iOS开发 多个textfield简单键盘处理

iOS开发以及好多年了,但是对于键盘处理从未认真的想一个简单的办法来处理,网上有大把的方法,有些写的并不是那么详细,实际使用起来会出各种幺蛾子,现在找到一个简单的模式,把这些代码考进textField所在的ViewController即可,因为时间关系,只做了textField的简单归纳,当然对于textView也是有效的,废话不多说,开始吧:

1,当然是要注册键盘的通知方法

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillChange:)name:UIKeyboardWillChangeFrameNotificationobject:nil];

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];

2,不要忘了取消注册通知,否则你会发现很蛋疼的,键盘会错乱

- (void)dealloc{

[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillChangeFrameNotificationobject:nil];

[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];

}

3关键的来了,通知对应的方法

3.1键盘弹起时,将被遮挡区域露出来,我们唯一需要做的一件事就是将当前控制器内的所有textField放进数组

- (void)keyboardWillChange:(NSNotification*) notification{

//获取键盘通知信息

NSDictionary*info = [notificationuserInfo];

//获取键盘弹出时长

CGFloatduration = [[infoobjectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue];

//获取键盘Rect

CGRectendKeyboardRect = [[infoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];

CGSizekeyboardSize = endKeyboardRect.size;

//self.view动画

[UIViewanimateWithDuration:durationanimations:^{

//键盘的高度

CGFloatkeyBoardToBottom = keyboardSize.height;

//获取当前输入框控件

UITextField*currentTF;

//这个里面放入当前控制器对应的textFIeld数组即可,就是这个数组

for(UITextField*TFin@[_oldPassWordTF,_nowPassWordTF,_confirmPassWordTF]) {

if([TFisFirstResponder]) {

currentTF = TF;

};

}

//控件底部到self.view底部的距离

CGFloatviewToBottom;

//定义输入框与键盘距离

CGFloatspaceHeight =10;

viewToBottom =self.view.frame.size.height-CGRectGetMaxY(currentTF.frame) - spaceHeight;

//键盘遮挡区域高度

CGFloatoffSet = viewToBottom -keyBoardToBottom;

if(offSet <0) {

//导航条高度

CGFloatnavHeight =64;

//self.view应该上移的高度

self.view.frame=CGRectMake(0, offSet + navHeight,self.view.bounds.size.width,self.view.bounds.size.height);

}

}completion:^(BOOLfinished) {

}];

}

3.2取消键盘时,self.view恢复原样

- (void)keyboardWillHide:(NSNotification*) notification{

//获取键盘通知信息

NSDictionary*info = [notificationuserInfo];

//获取键盘弹出时长

CGFloatduration = [[infoobjectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue];

[UIViewanimateWithDuration:durationanimations:^{

CGFloatnavHeight =64;

self.view.frame=CGRectMake(0, navHeight,self.view.bounds.size.width,self.view.bounds.size.height);

}completion:^(BOOLfinished) {

}];

}

时间比较紧,有时间再弄个通用的,如果有更好的想法或有不足之处可联系我,791903178qq,谢谢

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,238评论 1 6
  • 1、设置UILabel行间距 NSMutableAttributedString*attrString=[[NSM...
    iOS祎阅读 2,420评论 0 0
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    FF_911阅读 1,520评论 0 3
  • 驯化一词来自于,人和动物之间的关系,人们一直以来认为是人把狼驯化成了狗。你给狗盖窝,整天给它食物,还得给它洗澡,狼...
    新自由阅读 199评论 0 0
  • 我多么想为你写下一句 可下一个音符是不适合我的 急促的旋律 你有点难过 那样的情景起伏着 让你变得不快乐 我也失语...
    乂弈阅读 273评论 0 1

友情链接更多精彩内容