keyboard 監聽 改變高度

Swift:html

1.註冊通知  監聽ios

       NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardChangeFrame:", name: UIKeyboardDidChangeFrameNotification, object: nil);api

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil);app

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHidden:", name: UIKeyboardWillHideNotification, object: nil)iphone

2.實現三個方法,change若是是數字鍵盤,用不到可爲裏面不寫東西ide

    func keyboardWillShow(aNotification:NSNotification){佈局

        keyboardStatus = KeyBoardStatus.toShow動畫

        let moveFrame = CGRectMake(0, WXDevice.height - colorSizeVMoveHeight - CGRectGetHeight(self.frame), WXDevice.width, CGRectGetHeight(self.frame))ui

        

        UIView.animateWithDuration(0.25, animations: { () -> Void inspa

            self.frame = moveFrame

            }) { (animated) -> Void in

                

        }

    }


    func keyboardWillHidden(notification:NSNotification){

        UIKeyboardAnimationDurationUserInfoKey

        

        let moveFrame = CGRectMake(0, WXDevice.height - CGRectGetHeight(self.frame), WXDevice.width, CGRectGetHeight(self.frame))

        UIView.animateWithDuration(0.25, animations: { () -> Void in

            self.frame = moveFrame

            }) { (animated) -> Void in

        }

        if notification.userInfo != nil{

            keyboardStatus = KeyBoardStatus.toHidden

            

            let keyboardInfo: NSDictionary = notification.userInfo!;

            if let interval:Double = keyboardInfo.valueForKey(UIKeyboardAnimationDurationUserInfoKey) as? Double

            {

                UIView.animateWithDuration(interval, animations: { () -> Void in

                    self.contentScrollV.contentSize = CGSizeMake(WXDevice.width, self.preContentSize.height)

                    let yOffset = (self.preContentSize.height - CGRectGetHeight(self.contentScrollV.frame) >= 0) ?

                        (self.preContentSize.height - CGRectGetHeight(self.contentScrollV.frame)) : 0

                    self.contentScrollV.contentOffset = CGPointMake(0, yOffset)

                })

            }

        }

    }

    

    func keyboardChangeFrame(aNotification:NSNotification){

        if aNotification.userInfo != nil && keyboardStatus == KeyBoardStatus.toShow{

            let keyboardInfo: NSDictionary = aNotification.userInfo!;

            let keyboardFrame: NSValue = keyboardInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue;

            let _frame: CGRect = keyboardFrame.CGRectValue();

            

            var offset = (WXDevice.height - _frame.origin.y) - (CGRectGetHeight(sureBtn.frame) + 12)

            offset = offset - colorSizeVMoveHeight

            if offset > 0{

                contentScrollV.contentSize = CGSizeMake(WXDevice.width, preContentSize.height + offset)

                contentScrollV.scrollRectToVisible(CGRectMake(0, contentScrollV.contentSize.height - 1/WXDevice.scale, WXDevice.width, 1/WXDevice.scale), animated: false)

            }

        }

    }

    

3.移除

deinit {

        

        NSNotificationCenter.defaultCenter().removeObserver(self)

        DPrintln(__FUNCTION__)

    }


OC:

ios5 鍵盤高度變高覆蓋ui與獲取鍵盤高度的方法

背景: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];
相關文章
相關標籤/搜索