2017-08-04 75 views
0

我花了最多的一天上SO試圖弄清楚這一點上移,這是我的問題:的UITextView不使用鍵盤

我有一個UIScrollView與它內部的垂直堆疊視圖,內堆棧視圖我有幾個UIView s包含不同的東西。在底部視圖中,我有一個UITextView,當我開始輸入它時,理想情況下我喜歡向上移動,以便實際看到我正在輸入的內容。

實施方案中herehereherehere,並在此主題的所有其他類似的現有問題的工作,如果我在最後一個視圖,而不是一個UITextView一個UITextField,但我真的想有返回鍵/多行功能似乎需要UITextView

下面是我在代碼方面(每幾個上面的鏈接):

func registerForKeyboardNotifications() { 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: .UIKeyboardDidShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: .UIKeyboardWillHide, object: nil) 
} 

func keyboardWasShown(_ notification: NSNotification) { 

    guard let info = notification.userInfo, let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return } 

    let keyboardFrame = keyboardFrameValue.cgRectValue 
    let keyboardSize = keyboardFrame.size 

    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0) 
    scrollView.contentInset = contentInsets 
    scrollView.scrollIndicatorInsets = contentInsets 

} 

func keyboardWillBeHidden(_ notification: NSNotification) { 
    let contentInsets = UIEdgeInsets.zero 
    scrollView.contentInset = contentInsets 
    scrollView.scrollIndicatorInsets = contentInsets 
} 

我叫viewDidLoad()registerForKeyboardNotifications(),這是工作完美當我使用UITextField,但對於該功能無法使用UITextView。我能找到的最接近的SO問題是this one,但是這個問題從來沒有用涉及UITextView的可行解決方案來回答。我已經看過幾個帖子提到IQKeyboardManager,但我最好喜歡在本地修復此問題。

回答

0

我發現了一個解決方案,它結合了contentInsetsetContentOffset,但我確定它不是那裏最優雅的解決方案。然而,其他人是否遇到了麻煩一個textViewkeyBoardWasShown向上移動,你可以做到以下幾點:

設置您viewController當鍵盤最多可顯示通知;

func registerForKeyboardNotifications() { 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: .UIKeyboardDidShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: .UIKeyboardWillHide, object: nil) 
} 

func keyboardWasShown(_ notification: NSNotification) { 

    guard let info = notification.userInfo, let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return } 

    let keyboardFrame = keyboardFrameValue.cgRectValue 
    let keyboardSize = keyboardFrame.size 

    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0) 
    scrollView.contentInset = contentInsets 
    scrollView.scrollIndicatorInsets = contentInsets 

} 

func keyboardWillBeHidden(_ notification: NSNotification) { 
    let contentInsets = UIEdgeInsets.zero 
    scrollView.contentInset = contentInsets 
    scrollView.scrollIndicatorInsets = contentInsets 
} 

確保調用registerForKeyboardNotifications()viewDidLoad

這是根據您的特定設置而變化的地方。由於我有一個覆蓋大部分屏幕房地產的view,我真的只想讓我的scrollView(我將其製作爲IBOutlet)跳到足以顯示我的textView的一部分,以便用戶知道它在那裏並可編輯。這導致對處理鍵盤顯示/隱藏狀態的代碼的上述部分進行以下更改;

func keyboardWasShown(_ notification: NSNotification) { 

    guard let info = notification.userInfo, let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return } 

    let keyboardFrame = keyboardFrameValue.cgRectValue 
    let keyboardSize = keyboardFrame.size 

    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height - 50.0, 0.0) 
    scrollView.contentInset = contentInsets 
    scrollView.scrollIndicatorInsets = contentInsets 

    let bottom = keyboardSize.height - 60.0 
    scrollView.setContentOffset(CGPoint(x: 0, y: bottom), animated: true) 

} 

func keyboardWillBeHidden(_ notification: NSNotification) { 

    let contentInsets = UIEdgeInsets.zero 
    scrollView.contentInset = contentInsets 
    scrollView.scrollIndicatorInsets = contentInsets 

} 

重點項目是scrollView.setContentOffset部分。