惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
腾讯CDC
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
V
V2EX
Blog — PlanetScale
Blog — PlanetScale
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
量子位
F
Fortinet All Blogs
G
Google Developers Blog
T
The Blog of Author Tim Ferriss
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
J
Java Code Geeks
S
SegmentFault 最新的问题
D
Docker
博客园 - 司徒正美
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
M
MIT News - Artificial intelligence
博客园 - 【当耐特】

博客园 - jacktu

git 常用命令 请求处理常见tag语法 mysql执行group by提示only_full_group_by问题解决方法 go test命令(Go语言测试命令)完全攻略 mac下安装Homebrew提示Failed to connect to raw.githubusercontent.com port 443: Connection refused的解决办法【终极最简单版】 【转】Android类动态加载技术 Wireshark图解教程 asp.net 操作excel的一系列问题(未完待续) 不越狱无硬件,轻松搞定iPhone投影与录屏 Android 常见问题之Assets文件大小限制 会说话的TOM猫的原理是什么 android GC 【转】Android 3.1 r1 API中文文档(6)——ImageView android PorterDuffXfermode ,PorterDuff.Mode 使用 以及Porter-Duff规则详解 UIApplicationDelegate协议定义的方法说明 iPhone绘图关于QuartZ中绘制Line和虚线等 IOS开发网络篇之──ASIHTTPRequest详解 UILabel小结 【转】iPhone 模态对话框 立即返回结果
ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高...
jacktu · 2011-11-26 · via 博客园 - jacktu

背景:

  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 {

/*
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];
}

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

[[NSNotificationCenter defaultCenter] removeObserver:self];


ps:

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

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

- (void)textFieldDidBeginEditingUITextField *)textField

    {

        [self animateTextField: textField up: YES];

    }

    - (void)textFieldDidEndEditingUITextField *)textField

    {

        [self animateTextField: textField up: NO];

    }

    - (void) animateTextField: (UITextField*) textField up: (BOOL) up

    {

        const int movementDistance = 80; // tweak as needed

        const float movementDuration = 0.3f; // tweak as needed

        int movement = (up ? -movementDistance : movementDistance);

        [UIView beginAnimations: @"anim" context: nil];

        [UIView setAnimationBeginsFromCurrentState: YES];

        [UIView setAnimationDuration: movementDuration];

        self.view.frame = CGRectOffset(self.view.frame, 0, movement);

        [UIView commitAnimations];

    }