2016-07-27 77 views
0

我正在開發一個聊天應用程序,並且正在嘗試讓用戶交互式地關閉鍵盤。不幸的是,它不工作,因爲我預料到:關閉鍵盤的平滑過渡

image

(這是我開始拉鍵盤下來,我想我的文字,以便留在鍵盤的頂部不管是什麼。這是我曾嘗試:

viewDidLoad

textView.keyboardDismissMode = .interactive//I have tried the same thing with the table view as well, but it produces the same result. 

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 

在同一個班級:

func keyboardWillShow(_ aNotification: NSNotification) { 
    let info = aNotification.userInfo! 
    let keyboardFrame = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue() 
    UIView.animate(withDuration: 0.25) {() -> Void in 
     self.messageBottomConstraint.constant = keyboardFrame.height 
     self.view.layoutIfNeeded() 

    } 
} 

func keyboardWillHide(_ aNotification: NSNotification) { 
    UIView.animate(withDuration: 0.25) {() -> Void in 
     self.messageBottomConstraint.constant = 0 
     self.view.layoutIfNeeded() 

    } 
} 

如何在文本視圖位於鍵盤頂部而用戶正在解除它的情況下?謝謝!

回答

1

您應該覆蓋inputAccessoryViewinputAccessoryViewController屬性UIResponder(其中UIViewController繼承自)。這將允許您提供一個視圖/視圖控制器,該控制器在出現時會自動固定到鍵盤的頂部。

This blog post舉例說明了如何做到這一點。

+0

你能舉個例子嗎?博客文章在Obj-C中,我將其轉換成一些麻煩......謝謝! – penatheboss