是否可以動態獲取鍵盤的框架,實際上是它的高度?因爲我有一個UITextView
,並且我想根據鍵盤框架高度調整它的高度,當鍵盤的輸入方法改變時。如您所知,不同的輸入方法可能會有不同的鍵盤框架高度。動態獲取鍵盤的框架
回答
試試這個:
[[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..........
}
只要按照Apple的這個教程,你會得到你想要的。 Apple Documentation。爲了確定鍵盤所覆蓋的區域,請參考tutorial。
但是我怎麼能檢測到輸入法更改操作? – 2012-08-16 14:42:36
如果您是程序員,那麼您應該知道在任何給定時間您打開了哪個輸入軟鍵盤。 – doNotCheckMyBlog 2012-08-16 14:44:42
我希望我知道,但我不能。如果您知道如何檢測輸入法更改操作並獲取當前鍵盤的高度,請您告訴我。我真的很感激它:) – 2012-08-16 14:54:30
對於斯威夫特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!")
}
您可以將此代碼添加到包含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)
}
}
- 1. 獲取UItextview鍵盤動畫
- 2. 在窗口級別獲取鍵盤的框架
- 3. 獲取鍵盤框架沒有keyboardWillAppear用戶信息通知,iOS
- 4. 獲取修改鍵的狀態簡潔框架
- 5. Android - 獲取鍵盤按鍵
- 6. 鍵盤上的UIToolbar:更改框架?
- 7. 獲取EditTextPreference的軟鍵盤
- 8. 獲取自定義鍵盤中isSticky鍵的當前狀態
- 9. 如何獲取mac上鍵盤上某些鍵的狀態?
- 10. JavaScript - 從動態鍵/值獲取動態鍵/值的值
- 11. Telegram Bot動態鍵盤
- 12. 離子框架,鍵盤重疊表格
- 13. 獲取鍵盤上的鍵位置
- 14. 獲取鍵盤鍵的字符串
- 15. 自動完成錯誤的鍵盤框架
- 16. 關鍵字驅動框架 - 獲取錯誤java.lang.NullPointerException
- 17. Vb.net獲取鍵盤佈局
- 18. 在鍵盤上獲取MotionEvent
- 19. 從tkinter獲取鍵盤
- 20. 在iphone中獲取鍵盤
- 21. 如何在Mac OSX上獲取鍵盤按鍵狀態?
- 22. Android虛擬鍵盤KeyListener獲取虛擬鍵盤按鍵
- 23. 在Xcode中彈出鍵盤時向上移動框架
- 24. 移動視圖以同時重置框架和鍵盤
- 25. 什麼是動態框架而不是非動態框架?
- 26. 從ARM動態庫(框架)獲取參數列表?
- 27. 使用實體框架在MVC中獲取動態表名稱
- 28. 從實體框架獲取動態SQL結果
- 29. 獲取從鍵盤(或自定義鍵盤)輸入的文本
- 30. 如何動態獲取系統架構?
當在iOS8中使用不同高度/類型的鍵盤時,此功能特別有用。到目前爲止,簡單地硬編碼216(肖像)就可以做到這一點。謝謝。 ios 8上的 – n00bProgrammer 2014-09-20 07:02:50
它仍然迴響216,這是錯誤的。 – harshitgupta 2014-09-30 18:02:45
@Hector它也適用於iOS8嗎? – msmq 2014-10-20 12:09:06