2012-08-16 47 views
31

是否可以動態獲取鍵盤的框架,實際上是它的高度?因爲我有一個UITextView,並且我想根據鍵盤框架高度調整它的高度,當鍵盤的輸入方法改變時。如您所知,不同的輸入方法可能會有不同的鍵盤框架高度。動態獲取鍵盤的框架

回答

97

試試這個:

[[NSNotificationCenter defaultCenter] addObserver:self 
            selector:@selector(keyboardWasShown:) 
             name:UIKeyboardDidShowNotification 
             object:nil]; 

- (void)keyboardWasShown:(NSNotification *)notification 
{ 

// Get the size of the keyboard. 
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

//Given size may not account for screen rotation 
int height = MIN(keyboardSize.height,keyboardSize.width); 
int width = MAX(keyboardSize.height,keyboardSize.width); 

//your other code here.......... 
} 

Tutorial for more information

+1

當在iOS8中使用不同高度/類型的鍵盤時,此功能特別有用。到目前爲止,簡單地硬編碼216(肖像)就可以做到這一點。謝謝。 ios 8上的 – n00bProgrammer 2014-09-20 07:02:50

+2

它仍然迴響216,這是錯誤的。 – harshitgupta 2014-09-30 18:02:45

+0

@Hector它也適用於iOS8嗎? – msmq 2014-10-20 12:09:06

8

只要按照Apple的這個教程,你會得到你想要的。 Apple Documentation。爲了確定鍵盤所覆蓋的區域,請參考tutorial

+0

但是我怎麼能檢測到輸入法更改操作? – 2012-08-16 14:42:36

+0

如果您是程序員,那麼您應該知道在任何給定時間您打開了哪個輸入軟鍵盤。 – doNotCheckMyBlog 2012-08-16 14:44:42

+3

我希望我知道,但我不能。如果您知道如何檢測輸入法更改操作並獲取當前鍵盤的高度,請您告訴我。我真的很感激它:) – 2012-08-16 14:54:30

2

對於斯威夫特3的用戶,@Hector代碼(有一些添加物)將是:

在你viewDidLoad加上觀察員:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(_:)), name: .UIKeyboardDidShow , object: nil) 
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidHide(_:)), name: .UIKeyboardDidHide , object: nil) 

然後執行那些方法:

func keyboardDidShow(_ notification: NSNotification) { 
    print("Keyboard will show!") 
    // print(notification.userInfo) 

    let keyboardSize:CGSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size 
    print("Keyboard size: \(keyboardSize)") 

    let height = min(keyboardSize.height, keyboardSize.width) 
    let width = max(keyboardSize.height, keyboardSize.width) 

} 

func keyboardDidHide(_ notification: NSNotification) { 
     print("Keyboard will hide!") 
} 
0

您可以將此代碼添加到包含Swift 3中文本字段的視圖。這將使文本字段通過鍵盤上下移動。

private var keyboardIsVisible = false 
private var keyboardHeight: CGFloat = 0.0 

// MARK: Notifications 

private func registerForKeyboardNotifications() { 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
} 
private func deregisterFromKeyboardNotifications() { 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
} 

// MARK: Triggered Functions 

@objc private func keyboardWillShow(notification: NSNotification) { 
    keyboardIsVisible = true 
    guard let userInfo = notification.userInfo else { 
     return 
    } 
    if let keyboardHeight = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.height { 
     self.keyboardHeight = keyboardHeight 
    } 
    if !textField.isHidden { 
     if let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber, 
      let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber { 
      animateHUDWith(duration: duration.doubleValue, 
          curve: UIViewAnimationCurve(rawValue: curve.intValue) ?? UIViewAnimationCurve.easeInOut, 
          toLocation: calculateTextFieldCenter()) 
     } 
    } 
} 

@objc private func keyboardWillBeHidden(notification: NSNotification) { 
    keyboardIsVisible = false 
    if !self.isHidden { 
     guard let userInfo = notification.userInfo else { 
      return 
     } 
     if let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber, 
      let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber { 
      animateHUDWith(duration: duration.doubleValue, 
          curve: UIViewAnimationCurve(rawValue: curve.intValue) ?? UIViewAnimationCurve.easeInOut, 
          toLocation: calculateTextFieldCenter()) 
     } 
    } 
} 

// MARK: - Helpers 

private func animateHUDWith(duration: Double, curve: UIViewAnimationCurve, toLocation location: CGPoint) { 
    UIView.beginAnimations(nil, context: nil) 
    UIView.setAnimationDuration(TimeInterval(duration)) 
    UIView.setAnimationCurve(curve) 
    textField.center = location 
    UIView.commitAnimations() 
} 

private func calculateTextFieldCenter() -> CGPoint { 
    if !keyboardIsVisible { 
     return self.center 
    } else { 
     let yLocation = (self.view.frame.height - keyboardHeight)/2 
     return CGPoint(x: self.center.x, y: yLocation) 
    } 
}