打开键盘遮住View的问题解决方法-IOS开发

默认情况下打开键盘会遮住下面的view,带来一点点困扰,不过这不是什么大问题,我们使用点小小的手段就可以解决。

首先我们要知道键盘的高度是固定不变的,不过在IOS 5.0 以后键盘的高度貌似不是216了,不过不要紧,我们调整调整就是了:

iPhoneipad
竖屏(portrait)216264
横屏(landScape)140352

我们采取的方法就是在textField(有可能是其他控件)接收到弹出键盘事件时把self.view整体上移216px了(我们就以iPhone竖屏为例了)。

有关View的frame,origin,size之类的知识点不懂的请参看我的另一篇博文: <<有关View的几个基础知识点>>

首先我们要设置textField的代理,我们就设为当前控制器了。

textField,delegate=self;

然后我们在当前控制器实现下面三个委托方法:

- (void)textFieldDidBeginEditing:(UITextField *)textField { //当点触textField内部,开始编辑都会调用这个方法。textField将成为first responder NSTimeInterval animationDuration = 0.30f; CGRect frame = self.view.frame; frame.origin.y -=216; frame.size.height +=216; self.view.frame = frame; [UIView beginAnimations:@"ResizeView" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations]; }

- (BOOL)textFieldShouldReturn:(UITextField *)textField {//当用户按下ruturn,把焦点从textField移开那么键盘就会消失了 NSTimeInterval animationDuration = 0.30f; CGRect frame = self.view.frame; frame.origin.y +=216; frame.size. height -=216; self.view.frame = frame; //self.view移回原位置 [UIView beginAnimations:@"ResizeView" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations]; [textField resignFirstResponder]; }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值