How can I make a UIScrollView
scroll to the bottom within my code? Or in a more generic way, to any point of a subview?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
- How can I add media attachments to my push notific
Just an enhancement to the existing answer.
It takes care of the bottom inset as well (in case you're using that to adjust your scroll view when the keyboard is visible)
I found that
contentSize
doesn't really reflect the actual size of the text, so when trying to scroll to the bottom, it will be a little bit off. The best way to determine the actual content size is actually to use theNSLayoutManager
'susedRectForTextContainer:
method:To determine how much text actually is shown in the
UITextView
, you can calculate it by subtracting the text container insets from the frame height.Then it becomes easy to scroll:
Some numbers for reference on what the different sizes represent for an empty
UITextView
.I also found another useful way of doing this in the case you are using a UITableview (which is a subclass of UIScrollView):
In swift:
Swift version of the accepted answer for easy copy pasting:
Setting the content offset to the height of the content size is wrong: it scrolls the bottom of the content to the top of the scroll view, and thus out of sight.
The correct solution is to scroll the bottom of the content to the bottom of the scroll view, like this (
sv
is the UIScrollView):