How to make Textview track writing

Asked

Viewed 87 times

1

I need Textview to follow when I write because if this does not happen I end up writing under keyboard, I already gave an up in textview but it is very big and will end up being down anyway. how do I make the textView accompanied when I write inserir a descrição da imagem aqui

2 answers

0


This code I created translating from Objective-C to Swift worked for me.

I am not using scrollView and instead of moving the textView.frame, I’m using contentInset to track the cursor.

override func viewDidLoad() {
    registerForKeyboardNotification()
    textView.delegate = self
}

func registerForKeyboardNotification() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWasShown:"), name: UIKeyboardDidShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillBeHidden:"), name: UIKeyboardWillHideNotification, object: nil)
}

func keyboardWasShown(aNotification: NSNotification) {
    let info = aNotification.userInfo!
    let kbSize = (info[UIKeyboardFrameBeginUserInfoKey])!.CGRectValue.size
    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0)
    textView.contentInset = contentInsets
    textView.scrollIndicatorInsets = contentInsets
}

func keyboardWillBeHidden(aNotification: NSNotification) {
    let contentInsets = UIEdgeInsetsZero
    textView.contentInset = contentInsets
    textView.scrollIndicatorInsets = contentInsets
    textView.resignFirstResponder()
}

I believe to be very close to what we look for, it worked for me in Xcode 7 beta 4, I hope it helps.

0

I recently had a similar problem and used the library Tpkeyboardavoiding to solve it. This library automatically moves the text boxes off the keyboard.

To use it place the textViews inside a Tpkeyboardavoidingscrollview, which extends Uiscrollview. By nib just add a Uiscrollview object and change the class (Custom Class) for Tpkeyboardavoidingscrollview.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.