打开键盘遮住View的问题解决措施-iOS开发
发布时间:2021-11-24 12:38:07 所属栏目:教程 来源:互联网
导读:默认情况下打开键盘会遮住下面的view,带来一点点困扰,不过这不是什么大问题,我们使用点小小的手段就可以解决。 首先我们要知道键盘的高度是固定不变的,不过在IOS 5.0 以后键盘的高度貌似不是216了,不过不要紧,我们调整调整就是了: iphoneipad 竖屏(po
默认情况下打开键盘会遮住下面的view,带来一点点困扰,不过这不是什么大问题,我们使用点小小的手段就可以解决。 首先我们要知道键盘的高度是固定不变的,不过在IOS 5.0 以后键盘的高度貌似不是216了,不过不要紧,我们调整调整就是了: iphoneipad 竖屏(portrait): 216 264 横屏(landScape): 140 352 我们采取的方法就是在textField(有可能是其他控件)接收到弹出键盘事件时把self.view整体上移216px了(我们就以iPhone竖屏为例了)。 首先我们要设置textField的代理,我们就设为当前控制器了。 textField,delegate=self; 然后我们在当前控制器实现下面三个委托方法: - (void)textFieldDidBeginEditing:(UITextField *)textField { //当点触textField内部,开始编辑都会调用这个方法。textField将成为first responder NSTimeInterval animationDuration = 0.30f; <span style="white-space:pre"> </span>float width = self.view.frame.size.width; float height = self.view.frame.size.height; <span style="white-space:pre"> </span>CGRect frame = CGRectMake(0.0f, -216,width,height); <span style="white-space:pre"> </span>//self.view整体上移216,其实就是把origin负向偏移 <span style="white-space:pre"> </span>[UIView beginAnimations:@"ResizeView" context:nil]; <span style="white-space:pre"> </span>[UIView setAnimationDuration:animationDuration]; <span style="white-space:pre"> </span>self.view.frame = frame; [UIView commitAnimations]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField {//当用户按下ruturn,把焦点从textField移开那么键盘就会消失了 <span style="white-space:pre"> </span>NSTimeInterval animationDuration = 0.30f; float width = self.view.frame.size.width; float height = self.view.frame.size.height; CGRect frame = CGRectMake(0.0f,0.0f,width,height); //self.view移回原位置 [UIView beginAnimations:@"ResizeView" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations]; ![]() (编辑:通化站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐