2

I'm trying to implement a view very similar to Evernote's screen in which you add a New Note.

It seems like a UITableView embedded in a NavigationController. This tableview contains static cells (2 or 3) with the bottom one being a UITextView in which you add the content of the note, but when you scroll on the textView, the other cells that contain a textField and another control.

How can this be achieved? I know that Apple doesn't recommend a TextView inside a ScrollView, and doing it with table view it gets a bit weird with all the scrolling from the table and text view.

Here are some examples:

view (initial state) another one when scrolling

Any suggestions?

Thank you!

Otto Gutierrez
  • 447
  • 2
  • 5
  • 15
  • Look at the answer by Sodbileg Gansukh in this post. http://stackoverflow.com/questions/27652334/uitextview-inside-uiscrollview-with-autolayout – Harsh Aug 16 '16 at 02:14

2 Answers2

1

Firstly, They disabled text view scrolling and set its size to about screen size. Secondly, once text view's text is out of frame, expand it(calculate its size again).

Lumialxk
  • 6,239
  • 6
  • 24
  • 47
0

So I found my problem, when I was setting the constraints for the content view (view inside scrollview) I set an Equal value for its height. To fix it I just made that relationship to Greater or Equal than... it now expands.

The other problem now is that when showing the keyboard it is not scrolling to the text I tap to. (The insets are properly setup though)

// MARK: Notififations from the Keyboard
func didShowKeyboard (notification: NSNotification) {
    if momentTextView.isFirstResponder() {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.size.height, right: 0)
            scrollView.scrollIndicatorInsets = scrollView.contentInset
            let caretPosition = momentTextView.caretRectForPosition(momentTextView.selectedTextRange!.start)
            let newHeight = caretPosition.height * 1.5
            let newCaretPosition = CGRect(x: caretPosition.origin.x, y: caretPosition.origin.y, width: caretPosition.width, height: newHeight)
            scrollView.scrollRectToVisible(newCaretPosition, animated: true)

        }
    }
}

func willHideKeyboard (notification: NSNotification) {
    if momentTextView.isFirstResponder() {
        scrollView.contentInset = UIEdgeInsetsZero
        scrollView.scrollIndicatorInsets = UIEdgeInsetsZero
    }
}
Otto Gutierrez
  • 447
  • 2
  • 5
  • 15