2016-10-30 13 views
1

我有一個UITableView與「添加新項目」單元格的底部。當用戶點擊文本字段時,鍵盤出現並且tableview獲取內容插入,以使其底部始終位於鍵盤之上並且不隱藏在鍵盤之下。這已經幾乎完美了,但在鍵盤出現後,桌面視圖不再像以前那樣滾動到底部,它稍微高於底部,使得底部單元部分被鍵盤覆蓋。設置內容嵌入使UItableView滾動一點

設置內容的插圖,我註冊鍵盤的通知,並運行此代碼:

func adjustForKeyboard(notification: Notification) { 

    let userInfo = notification.userInfo! 

    let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 
    let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window) 

    if notification.name == Notification.Name.UIKeyboardWillHide { 
     tableView.contentInset = UIEdgeInsets.zero 
    } else { 
     tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0) 
    } 

    tableView.scrollIndicatorInsets = tableView.contentInset 
} 

這裏也是一些截圖:

After keyboard appeared

How it should look

回答

2
let indexPath = IndexPath(row: 0, section: tableView.numberOfSections - 1) 
    tableView.scrollToRow(at: indexPath, at: .top, animated: false) 

添加這兩行我的fu nction爲我部分固定它。我可以發誓,我之前嘗試過,但我不認爲我使用過沒有動畫的版本。也許這會有所不同。

相關問題