您的位置:首页 > 产品设计 > UI/UE

IOS解决键盘挡住UITextView的方法

2014-02-19 14:48 531 查看
想要解决这个问题,首先了解一些通知(notifications)

1.UIKeyboardWillShowNotification

当键盘准备显示的时候会发出这个通知,只要是可编辑的原件都有效

2.UIKeyboardDidShowNotification键盘完全显示后发出这个通知

3.UIKeyboardWillHideNotification键盘消失准备离开我们的视野的时候发出这个通知

4.UIKeyboardDidHideNotification键盘完全消失的时候发出这个通知

所以我们的办法就是需要知道键盘什么时候出现,在这个时刻我们再次定义我们的可编辑控件的大小,比如UITextView,UITextField等;

(PS:键盘通知里面包含有一个userInfo这个属性,该属性包含了键盘的尺寸,其中有一个UIKeyboardFrameEndUserInfoKey作为字典中的一个键值,指向的就是键盘完全显示的时候,键盘大小、尺寸)

NSValue *keyboardRectAsObject = [[paramNotification userInfo]
objectForKey:UIKeyboardFrameEndUserInfoKey];

通过上述代码获得显示完全的键盘属性

首先系统发出通知,我们做的就是监听通知,然后处理监听方法

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardDidShowNotification object:Nil];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hanleKeyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];

}

其中@selector方法如下
-(void)handleKeyboardDidShow:(NSNotification*)paramNotification
{
NSValue *keyboardRectAsObject = [[paramNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];

CGRect rect;
[keyboardRectAsObject getValue:&rect];

self.myTextView.contentInset = UIEdgeInsetsMake(0, 0, rect.size.height + 10, 0);
}

-(void)hanleKeyboardWillHide:(NSNotification*)paramNotification
{

self.myTextView.contentInset = UIEdgeInsetsZero;
}

好了,以上就是核心代码,祝大家编码愉快~~

(PS:原创文章,转载请标明出处)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uitextfield