iOS-當輸入框被鍵盤遮擋時讓整個view上移

來源:互聯網
上載者:User

標籤:ios   擷取   enter   tor   idt   undefined   obj   mat   連結   

    • 註冊鍵盤通知
    #pragma mark - 鍵盤通知- (void)addNoticeForKeyboard {    //註冊鍵盤出現的通知    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(keyboardWillShow:)                                                 name:UIKeyboardWillShowNotification object:nil];    //註冊鍵盤消失的通知    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(keyboardWillHide:)                                                 name:UIKeyboardWillHideNotification object:nil];}
    • 鍵盤代理事件
    ///鍵盤顯示事件- (void) keyboardWillShow:(NSNotification *)notification {    //擷取鍵盤高度,在不同裝置上,以及中英文下是不同的    CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;    //計算出鍵盤頂端到inputTextView panel底端的距離(加上自訂的緩衝距離INTERVAL_KEYBOARD)    CGFloat offset = (textView.frame.origin.y+textView.frame.size.height+INTERVAL_KEYBOARD) - (self.view.frame.size.height - kbHeight);    // 取得鍵盤的動畫時間,這樣可以在視圖上移的時候更連貫    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];    //將視圖上移計算好的位移    if(offset > 0) {        [UIView animateWithDuration:duration animations:^{            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);        }];    }}///鍵盤消失事件- (void) keyboardWillHide:(NSNotification *)notify {    // 鍵盤動畫時間    double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];    //視圖下沉恢複原狀    [UIView animateWithDuration:duration animations:^{        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);    }];}


    奮鬥的蝸牛
    連結:http://www.jianshu.com/p/4e235e952b0c
    來源:簡書

iOS-當輸入框被鍵盤遮擋時讓整個view上移

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.