您可以使用鍵盤通知滾動實現代碼如下起來
// Keyboard
func registerForKeyboardNotifications() {
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(keyboardWillShow),
name: UIKeyboardWillShowNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(keyboardWillHide),
name: UIKeyboardWillHideNotification,
object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
let keyboardHeight = keyboardSize.height
let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0)
tableView.contentInset = contentInsets
tableView.scrollIndicatorInsets = contentInsets
}
}
}
func keyboardWillHide(notification: NSNotification) {
tableView.contentInset = UIEdgeInsetsZero
tableView.scrollIndicatorInsets = UIEdgeInsetsZero
}
他們你打電話的viewDidLoad函數
override func viewDidLoad() {
super.viewDidLoad()
registerForKeyboardNotifications()
}
我已經在其他地方實現這一點。是否有可能使TableViewController處理它而不是使用鍵盤通知? – MrDank
@Watson你可以使它在ScrollViewDelegate中,沒問題。 TableView是ScrollView的子類。 –