我在我的項目中有一個文本視圖,我想當用戶點擊文本字段時,它向上移動等於鍵盤的高度我使用此代碼獲取鍵盤的高度,它給我的鍵盤大小如何偏移文本視圖等於鍵盤在swift 3中的高度?
static var sizeForOffsetKeyboard = CGFloat()
var heightKeyboard : CGFloat?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardShown(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
}
func keyboardShown(notification: NSNotification) {
if let infoKey = notification.userInfo?[UIKeyboardFrameEndUserInfoKey],
let rawFrame = (infoKey as AnyObject).cgRectValue {
let keyboardFrame = view.convert(rawFrame, from: nil)
self.heightKeyboard = keyboardFrame.size.height
levelChatViewController.sizeForOffsetKeyboard = heightKeyboard!
print(levelChatViewController.sizeForOffsetKeyboard)
// Now is stored in your heightKeyboard variable
}
}
但我沒有得到的結果和TextView中不會與鍵盤
所以這裏的高度拉昇是偏移的TextView代碼
func textViewDidBeginEditing(_ textView: UITextView) {
animateViewMoving(up: true, moveValue: levelChatViewController.sizeForOffsetKeyboard)
}
可以使用的UITableViewController自動滾動。檢查我的答案在這裏https://stackoverflow.com/questions/45233089/move-textfield-up-when-keyboard-displays/45233564#45233564 – luckyShubhra
我用https://stackoverflow.com/questions/26070242/move-view- with-keyboard-using-swift and its ok但是,當文本字段正在編輯並且用戶更改鍵盤時,文本字段將不會隨新鍵盤移動 –
當基於當前選擇哪個文本文件的鍵盤時,您必須繼續添加高度。你可以使用UITableViewController,你可以用零代碼實現你想要的東西。 – luckyShubhra