大佬教程收集整理的这篇文章主要介绍了iOS开发中的键盘高度变化处理,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
IPAD键盘高度:
porTrait 264 landscape 352.
iPhone键盘高度: PorTrait 216 Landscape 140 NsnotificationCenter:键盘出现、消失时的通知
UIKeyboardWillShowNotification; UIKeyboardDidShowNotification UIKeyboardWillHideNotificationUIKeyboardDidHideNotification;
背景:
ios5之前,iphone上的键盘的高度是固定为216.0px高的,中文汉字的选择框是悬浮的,所以不少应用都将此高度来标注键盘的高度。
可是在ios5中,键盘布局变了,尤其是中文输入时,中文汉字选择框就固定在键盘上方,这样就使得原本与键盘紧密贴合的界面视图被中文汉字选择框给覆盖住了。一方面影响了界面的美观,另一方面,如果被覆盖的部分就是文本输入框的话,用户就无法看到输入的内容了。因此这个问题就必须得解决了。
解决方法:
其实在一开始使用216.0px这个固定值来标注键盘的高度就是错误的。因为在ios3.2以后的系统中,苹果就提供了键盘使用的api以及demo程序——“KeyboardAccessory”。
处理键盘事件的正确方法是这样的:(包括获取键盘的高度以及键盘弹出和消失动画的时间)
[[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
获取到当前keyboard的高度以及动画时间,然后对视图进行对应的操作即可。
#pragma mark -
#pragma mark Responding to keyboard events
- (void)keyboardWillShow:(Nsnotification *)notification {
/*
Reduce the size of the text view so that it's not obscured by the keyboard.
Animate the resize so that it's in sync with the appearance of the keyboard.
*/
NSDictionary *userInfo = [notification userInfo];
Get the origin of the keyboard when it's displayed.
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
CGRect keyboardRect = [aValue CGRectValue];
Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
Animate the resize of the text view's frame in sync with the keyboard's appearance.
[self moveInputBarWithKeyboardHeight:keyboardRect.size.height withDuration:animationDuration];
} - (void)keyboardWillHide:(Nsnotification *)notification {
NSDictionary* userInfo = [notification userInfo];
Restore the size of the text view (fill self's view).
Animate the resize so that it's in sync with the disappearance of the keyboard.
*/
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration];
}
以上是大佬教程为你收集整理的iOS开发中的键盘高度变化处理全部内容,希望文章能够帮你解决iOS开发中的键盘高度变化处理所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。