600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)

ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)

时间:2019-12-30 10:15:20

相关推荐

ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)

背景:

ios5之前,iphone上的键盘的高度是固定为216.0px高的,中文汉字的选择框是悬浮的,所以不少应用都将此高度来标注键盘的高度(包括米聊也是这么做的)。

可是在ios5中,键盘布局变了,尤其是中文输入时,中文汉字选择框就固定在键盘上方,这样就使得原本与键盘紧密贴合的界面视图被中文汉字选择框给覆盖住了。一方面影响了界面的美观,另一方面,如果被覆盖的部分就是文本输入框的话,用户就无法看到输入的内容了。因此这个问题就必须得解决了。

解决方法:

其实在一开始使用216.0px这个固定值来标注键盘的高度就是错误的。因为在ios3.2以后的系统中,苹果就提供了键盘使用的api以及demo程序——“KeyboardAccessory”。

处理键盘事件的正确方法是这样的:(包括获取键盘的高度以及键盘弹出和消失动画的时间)

1)在要使用键盘的视图控制器中,接收键盘事件的通知:

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

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

// 键盘高度变化通知,ios5.0新增的

#ifdef __IPHONE_5_0

float version = [[[UIDevice currentDevice] systemVersion] floatValue];

if (version >= 5.0) {

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];

}

#endif

2)然后添加键盘事件的处理代码:

获取到当前keyboard的高度以及动画时间,然后对视图进行对应的操作即可。

#pragma mark -

#pragma mark Responding to keyboard events

- (void)keyboardWillShow:(NSNotification *)notification {

NSDictionary *userInfo = [notification userInfo];

NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

CGRect keyboardRect = [aValue CGRectValue];

//DLog(@"keyboard height %f", CGRectGetHeight(keyboardRect));

[UIView animateWithDuration:.25f

animations:^{

self.inputContainerView.frame = CGRectMake(0

, CGRectGetHeight([UIScreen mainScreen].bounds) - CGRectGetHeight(keyboardRect)

- 160 /*- NAVBAR_HEIGHT*/

, 320, 300);

}

completion:nil];

}

- (void)keyboardWillHide:(NSNotification *)notification {

[UIView animateWithDuration:.25f

animations:^{

self.inputContainerView.frame = CGRectMake(0

, /*SCREEN_HEIGHT - 100*/300

, 320

, 30);

}

completion:^(BOOL finished){

}];

}

3)在视图控制器消除时,移除键盘事件的通知:

[[NSNotificationCenter defaultCenter] removeObserver:self];

ps:

ios5隐藏功能分享——“字典”功能(英英字典):

在任何输入框中选中一个英文单词,此时会有选择项“复制”,“删除”...等,还有一个向右的箭头,点击这个向右的箭头后,就会出现“定义”选项,点击这个“定义”按钮即会弹出这个英语单词的英文解释。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。