IOS開發—擷取ios軟鍵盤高度

來源:互聯網
上載者:User

標籤: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軟鍵盤高度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.