关于键盘遮住文本框的处理,以用户登录为例。有两种方法,第二种比较简单。
经实测发现,ipad mini和ipad air的宽高不一样,正好相反。mini宽度为1024,高度为768;air宽度为768,高度为1024。
所以在适配这两种设备时还需判断。
1 CGFloat deviceHeight = [UIScreen mainScreen].bounds.size.height; 2 CGFloat deviceWidth = [UIScreen mainScreen].bounds.size.width; 3 CGFloat backGroundViewW = 0; 4 //设备为mini 5 if (deviceWidth > deviceHeight) { 6 backGroundViewW = deviceWidth; 7 } else { //设备为air 8 backGroundViewW = deviceHeight; 9 }
下面是如何解决键盘遮住文本框的问题。
方法一:
1 - (void)viewDidLoad{ 2 [super viewDidLoad]; 3 4 //添加登录界面 5 [self setupLoginView]; 6 7 //监听文本框 8 [self listenTextField]; 9 10 //单击其它区域隐藏键盘 11 [self hideKeyBoardWithClickArea]; 12 }
1 //账号、密码输入框的监听 2 -(void)listenTextField{ 3 [self.accountField addTarget:self action:@selector(textFieldBeginEdit:) forControlEvents:UIControlEventEditingDidBegin]; 4 [self.accountField addTarget:self action:@selector(textFieldEndEdit:) forControlEvents:UIControlEventEditingDidEnd]; 5 [self.passwordField addTarget:self action:@selector(textFieldBeginEdit:) forControlEvents:UIControlEventEditingDidBegin]; 6 [self.passwordField addTarget:self action:@selector(textFieldEndEdit:) forControlEvents:UIControlEventEditingDidEnd]; 7 } 8 9 //开始编辑时,整体上移 10 -(void)textFieldBeginEdit:(UITextField *)textField{ 11 [self moveView:-150]; 12 } 13 14 //结束编辑后,移回原来位置 15 -(void)textFieldEndEdit:(UITextField *)textField{ 16 [self moveView:150]; 17 }
1 //view移动的具体方法 2 -(void) moveView:(float)move{ 3 NSTimeInterval animationDuration = 0.3f; 4 CGRect frame = self.view.frame; 5 CGFloat deviceHeight = [UIScreen mainScreen].bounds.size.height; 6 CGFloat deviceWidth = [UIScreen mainScreen].bounds.size.width; 7 //判断设备为ipad air,并且是否倒置 8 if (deviceHeight > deviceWidth && self.interfaceOrientation==4) { 9 //view的X轴上移,pad air 10 frame.origin.x += move; 11 } else if (deviceHeight > deviceWidth && self.interfaceOrientation==3){ 12 frame.origin.x -= move; 13 } else { //为mini 14 frame.origin.y += move; 15 } 16 17 self.view.frame = frame; 18 [UIView beginAnimations:@"2" context:nil];// 2是动画的标示, 19 [UIView setAnimationDuration:animationDuration]; 20 self.view.frame = frame; 21 [UIView commitAnimations]; 22 }
1 //点击其它区域隐藏键盘 2 -(void)hideKeyBoardWithClickArea{ 3 UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyBoard)]; 4 //确认是单击其他区域,而不是双击等其它操作 5 gesture.numberOfTapsRequired = 1; 6 [self.view addGestureRecognizer:gesture]; 7 8 //点击键盘右下角“隐藏键盘”那个按钮,关闭键盘并且恢复view的位置 9 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyBoard) name:UIKeyboardWillHideNotification object:nil]; 10 } 11 12 -(void)hideKeyBoard{ 13 [self.accountField resignFirstResponder]; 14 [self.passwordField resignFirstResponder]; 15 [self resumeView]; 16 } 17 18 //恢复view的原来位置 19 -(void)resumeView{ 20 NSTimeInterval animationDuration=0.5f; 21 [UIView beginAnimations:@"3" context:nil]; 22 [UIView setAnimationDuration:animationDuration]; 23 [UIView commitAnimations]; 24 }
方法二:
这种方法很简单,也很偷懒,一劳永逸。
就是从github上搜IQKeyboardManager,直接放进项目中即可!
***********************************************这是更正线***************************************************************
开篇说是ipad air和ipad mini的高宽不一样,实际上我忽略了各自的版本。mini的时8.1,air的是7.1。
所以高宽不一样应该是由于IOS版本不一致造成的,而不是真实设备差异!但上述代码还是可以用的。
以后得注意。:)