码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-05-08 18:14:03      阅读:122      评论:0      收藏:0      [点我收藏+]

标签: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>

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

标签:ios开发   ios软键盘   获取键盘高度   

原文地址:http://blog.csdn.net/lotheve/article/details/45582541

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!