IOS开发—获取ios软键盘高度

- (void)viewDidLoad
{
    [super viewDidLoad];

    //增加监听,当键盘出现或改变时收出消息
<pre name="code" class="objc"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: rgb(0, 0, 0);">    [[</span><span style="color: rgb(112, 61, 170);">NSNotificationCenter</span><span style="color: rgb(0, 0, 0);"> </span>defaultCenter<span style="color: rgb(0, 0, 0);">] </span>addObserver<span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(187, 44, 162);">self</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">                                             <span style="color: rgb(61, 29, 129);">selector</span>:<span style="color: rgb(187, 44, 162);">@selector</span>(keyboardDidShow:)</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">                                                 <span style="color: rgb(61, 29, 129);">name</span>:<span style="color: rgb(112, 61, 170);">UIKeyboardWillShowNotification</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">                                               <span style="color: rgb(61, 29, 129);">object</span>:<span style="color: rgb(187, 44, 162);">nil</span>];</p>

//增加监听,当键退出时收出消息

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: rgb(0, 0, 0);">    [[</span><span style="color: rgb(112, 61, 170);">NSNotificationCenter</span><span style="color: rgb(0, 0, 0);"> </span>defaultCenter<span style="color: rgb(0, 0, 0);">] </span>addObserver<span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(187, 44, 162);">self</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">                                             <span style="color: rgb(61, 29, 129);">selector</span>:<span style="color: rgb(187, 44, 162);">@selector</span>(keyboardDidHide:)</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">                                                 <span style="color: rgb(61, 29, 129);">name</span>:<span style="color: rgb(112, 61, 170);">UIKeyboardWillHideNotification</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">                                               <span style="color: rgb(61, 29, 129);">object</span>:<span style="color: rgb(187, 44, 162);">nil</span>];</p>

}

//当键盘出现或改变时调用
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">- (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">void</span>)keyboardDidShow:(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSNotification</span> *)notification{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSDictionary</span> *userInfo = [notification <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">userInfo</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo; color: rgb(112, 61, 170);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    </span>CGSize<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> keyboardSize = [[userInfo </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">objectForKey</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span>UIKeyboardFrameBeginUserInfoKey<span style="font-variant-ligatures: no-common-ligatures; color: #000000">] </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">CGRectValue</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">].</span>size<span style="font-variant-ligatures: no-common-ligatures; color: #000000">;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">_keyBoardHeight</span> = keyboardSize.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">height</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo; color: rgb(49, 89, 93);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    [</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>changeViewYByShow<span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">
</p><pre name="code" class="objc"><pre name="code" class="objc">//当键盘隐藏时调用

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">- (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">void</span>)keyboardDidHide:(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSNotification</span> *)notification{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo; color: rgb(79, 129, 135);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    </span>_keyBoardHeight<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo; color: rgb(49, 89, 93);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    [</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>changeViewYByHide<span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">}</p>

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo; color: rgb(120, 73, 42);">#pragma mark - private methods</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">- (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">void</span>)changeViewYByShow{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo; color: rgb(61, 29, 129);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    [</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">UIView</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>animateWithDuration<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">0.2</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>animations<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:^{</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">        <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">CGRect</span> rect = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">view</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">frame</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">        rect.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">origin</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">y</span> -= <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">keyBoardHeight</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">        <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">view</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">frame</span> = rect;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">    }];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">}</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">- (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">void</span>)changeViewYByHide{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">CGRect</span> rect = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">view</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">frame</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">    rect.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">origin</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">y</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">64</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">view</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">frame</span> = rect;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo;">}</p>
				
时间: 2024-08-01 12:09:25

IOS开发—获取ios软键盘高度的相关文章

Android获取虚拟软键盘高度

1 public static int getDpi(Context context) { 2 int dpi = 0; 3 WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 4 Display display = windowManager.getDefaultDisplay(); 5 DisplayMetrics displayMetrics = ne

IOS下 input 被软键盘方案遮盖问题解决

前言: 并没有完美解决 ! 场景: 最近在做企业微信H5的一个项目,里面有个动态列表页,开始输入框是隐藏的,点击评论才会出现并让 input 聚焦.经过测试在安卓上面应该没什么问题,但是iOS上面会出现 input 被软键盘遮盖或者半遮盖.甚至发送按钮不能点击的状况. 总的来说问题有点小严重. 查阅百度: 好的,百度一搜,哇!解决方案很多,我就放心的去抽了一根烟,想着分分钟完事.BUT 经过试验 百度 “ios 下input 被软键盘遮盖” 前两页没有一个能解决我的问题的!!! 不过收获还是有的

IOS开发学习笔记-(2)键盘控制,键盘类型设置,alert 对话框

一.关闭键盘,放弃第一响应者,处理思路有两种 ① 使用文本框的 Did End on Exit 绑定事件 ② UIControl on Touch 事件 都去操作 sender 的  resignFirstResponder #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *txtUserName; @pro

iOS开发- 获取精确剩余电量

[UIDevice currentDevice].batteryMonitoringEnabled = YES; double deviceLevel = [UIDevice currentDevice].batteryLevel; 获取当前剩余电量, 我们通常采用上述方法.这也是苹果官方文档提供的. 它返回的是0.00-1.00之间的浮点值.  另外, -1.00表示模拟器. 貌似这个方法不错, 也很简单. 但是仔细观察它的返回值, 我们可以发现. 它是以0.05递变的. 折算成100% 也就

【好程序员笔记分享】——iOS开发之纯代码键盘退出

-iOS培训,iOS学习-------型技术博客.期待与您交流!------------ iOS开发之纯代码键盘退出(非常简单)     iOS开发之纯代码键盘退出 前面说到了好几次关于键盘退出的,但是最近开始着手项目的时候却闷了,因为太多了,笔者确实知道有很多中方法能实现,而且令我影响最深的就是 EndEditing,但是因为即有textView,又有TextField而且他们各有不同的方法,虽然笔者现在搞懂了,但是不知道什么时候又不记得 了,而且虽然感觉很简单现在感觉很简单的样子,但是对于没

iOS开发 - 获取真机沙盒数据

今天要获取之前真机测试时写入沙盒的数据, 本来以为挺麻烦的. 后来捣腾了一下, 才知道原来这么简单... 下面直接看具体步骤. 前提: 真机已经通过USB和你的电脑连接上了! 1.进入Organizer 2.选择你的应用. 并导出数据.(点击Download) 3.保存数据到相应位置.(点击Save) 4.打开, 查看数据. 保存完毕后, 你后发现一个这样的文件. 选中它, 右键---->显示包内容. 然后进入------->AppData ------>Documents. 即可看到你

IOS开发 获取当前视图所在的类名

NSString *stringClass = NSStringFromClass([(UINavigationController *)self.window.rootViewController topViewController].class); IOS开发 获取当前视图所在的类名

iOS开发系列--IOS程序开发概览ios基础

iOS开发系列--IOS程序开发概览 2014-08-04 19:42 by KenshinCui, 72273 阅读, 81 评论, 收藏,  编辑 概览 终于到了真正接触IOS应用程序的时刻了,之前我们花了很多时间去讨论C语言.ObjC等知识,对于很多朋友而言开发IOS第一天就想直接看到成果,看到可以运行的IOS程序.但是这里我想强调一下,前面的知识是你日后开发IOS的基础,没有那些知识你开发IOS会很痛苦,现在很多开发人员做开发都是一知半解,程序质量确实令人担忧,所以还是希望大家能够熟练掌

iOS开发中如何在键盘弹出时改变View的高度

在iOS开发的时候有两个经常要用到的控件UITextfield跟UITextView,我们输入内容基本是通过这两个控件进行的,但是有时候会遇到这样的问题:在点击输入之后弹出键盘遮盖住了输入框,可以通过以下办法解决: 添加通知监听键盘的弹出跟隐藏 //监听键盘弹出和隐藏 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillSho