iOS - 判断设备类型/第一响应者

来源:互联网 发布:免费x站最新域名升级 编辑:程序博客网 时间:2024/06/06 04:48

加班解决本周五品质检测的问题中,遇到这样一个问题,当我用4s模拟器跑的时候,键盘会挡住我的输入框,所以相应处理是要把view上移,但是其他设备,比如(5s/6s)屏幕比较大的设备,就不会被遮挡住输入框,此时不用理会,要解决的仅仅是4或4s设备。

我在pch文件里,做了相应的宏定义,主要以屏幕宽度来判断:

//判断机型#define iPhone4s ([UIScreen mainScreen].bounds.size.height == 480 ? YES : NO)#define iPhone5s ([UIScreen mainScreen].bounds.size.height == 568 ? YES : NO)#define iPhone6s ([UIScreen mainScreen].bounds.size.height == 667 ? YES : NO)#define iPhone6plus ([UIScreen mainScreen].bounds.size.height == 736 ? YES : NO)


我在解决问题中,出现了第二个问题,比如我的界面里有A和B两个输入框,当当前是A输入框的时候,键盘不会挡住A输入框,所以屏幕的view不需要上移,但当在B输入框的时候,键盘会挡住,此时就要view上移。

此时我用的是isFirstResponder 判断是否为第一响应者的方法。

假设B输入框为第一响应者,那么就view上移,附上我两个方法:  上移 和 下移

-(void)showKeyBoardMarkViewUp{            CGRect rect = self.view.frame;    rect.origin.y = -145 + 64 ;        [UIView animateWithDuration:0.25 animations:^{                self.view.frame = rect;            } completion:nil];        }


-(void)hideKeyBoardMarkViewDown{        //失去所有第一响应,键盘下移    [self.view endEditing:YES];        CGRect rect = self.view.frame;    rect.origin.y = 64;        [UIView animateWithDuration:0.2 animations:^{                self.view.frame = rect;            } completion:nil];    }



再附上,系统监听键盘的显示和隐藏的通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow) name:UIKeyboardWillShowNotification object:nil];                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHide) name:UIKeyboardWillHideNotification object:nil];




本周分享内容比较简单,哈哈。



0 0
原创粉丝点击