UIScrollView with sticky footer UIView and dynamic height content

前端 未结 3 499
[愿得一人]
[愿得一人] 2020-12-24 03:46

Challenge time!

Imagine we have 2 content views:

  1. UIView with dynamically height content (expandable UITextView) = RED
  2. UIView as a footer = BLU
3条回答
  •  旧巷少年郎
    2020-12-24 04:33

    Instead of using a UIScrollView you would very likely be better off with a UITableView. It also might be better to not using auto-layout. At least, I've found it better to not use it for these sorts of manipulations.

    Look into the following:

    • UITextView textViewDidChange
      • Change the size of the text view using sizeThatFits (limiting width and using FLT_MAX for height). Change the frame, not the contentSize.
      • Call UITableView beginUpdates/endUpdates to update the table view
      • Scroll to the cursor
    • UIKeyboardWillShowNotification notification
      • On NSNotification that comes through, you can call userInfo (a Dictionary), and the key UIKeyboardFrameBeginUserInfoKey. Reduce the frame of the table view based on the height of the size of the keyboard.
      • Scroll to cursor again (since the layouts will have all changed)
    • UIKeyboardWillHideNotification notification
      • The same as the show notification, just opposite (increasing the table view height)

    To have the footer view stick to the bottom, you could add an intermediate cell to the table view, and have it change size depending on the size of the text and whether the keyboard is visible.

    The above will definitely require some extra manipulation on your part - I don't fully understand all of your cases, but it should definitely get you started.

提交回复
热议问题