IOS UITextView. Text jump after UIKeyboardDidHideNotification

I have an editable UITextView and the keyboard mute mode is interactive. Also my controller listens for two notifications: UIKeyboardWillShowNotification, UIKeyboardWillHideNotification.

func keyboardWillShow(notification: NSNotification) {


    if let userInfo = notification.userInfo {

            var insets = self.textView.contentInset;
            let rect = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue() ?? CGRectZero
            insets.bottom = (rect.size.height - (CGRectGetHeight(self.view.frame) - CGRectGetMaxY(self.textView.frame)))
            self.textView.contentInset = insets
            self.textView.scrollIndicatorInsets = insets
    }
}


func keyboardWillHide(notification: NSNotification) {
        self.textView.contentInset = UIEdgeInsetsZero
        self.textView.scrollIndicatorInsets = UIEdgeInsetsZero 
}

This stuff works fine if the text in the UITextView does not contain blank lines. If so, contentOffsetmoves to another random place.

I'm not sure if this is a bug in iOS 7+, or am I doing something wrong. If this is not a mistake, how does it happen freely, without spasmodic behavior?

Thank you for your help.

+4
source share
2 answers

100% , , , . ( , !!)

4 . UITextView UITableView ( UITableViewCell, UITableView, ). UITextView .

UITextView UITableView , . ( UITextView delegate)

func textViewDidChange(textView: UITextView) {
    // Change textView height
    self.textView.sizeToFit()

    UIView.setAnimationsEnabled(false)
    self.tableView.beginUpdates()
    self.tableView.endUpdates()
    UIView.setAnimationsEnabled(true)
}

UITableView , UITextView .

func textViewDidBeginEditing(textView: UITextView) {
    // Delay the following line so that it works properly
    let delay = 0.005 * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue()) {
        var rect = self.textView.caretRectForPosition(self.textView.selectedTextRange?.end)
        var changedRect = CGRectMake(rect.origin.x, rect.origin.y, rect.width, rect.height+3)

        self.tableView.scrollRectToVisible(changedRect, animated: true)
    }
}

UITableView contentInset scrollIndicatorInsets keyboardWillShow keyboardWillHide, .

+1

, , UITextView {0, 0}. , , .

, UITextView contentOffset {0, 0}, , , , 3 ( , {0, 3605}, {0, 3605} {0, 3605} ).

, , :

textview.layoutManager.allowsNonContiguousLayout = NO;

. , :)

+1

Source: https://habr.com/ru/post/1568759/


All Articles