You can use the UIScrollView's setContentOffset:animated: function to scroll to any part of the content view. Here's some code that would scroll to the bottom, assuming your scrollView is self.scrollView:
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):
This ensures the bottom edge of your content is always at or above the bottom edge of the view. The MIN(0, ...) is required because UITableView (and probably UIScrollView) ensures contentOffsetY >= 0 when the user tries to scroll by visibly snapping contentOffsetY = 0. This looks pretty weird to the user.
It looks like all of the answers here didn't take the safe area into consideration.
Since iOS 11, iPhone X had a safe area introduced. This may affect the scrollView's contentInset.
For iOS 11 and above, to properly scroll to the bottom with the content inset included. You should use adjustedContentInset instead of contentInset. Check this code:
Didn't work for me, when I tried to use it in UITableViewController on self.tableView(iOS 4.1), after adding footerView. It scrolls out of the borders, showing black screen.
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 the NSLayoutManager's usedRectForTextContainer: method:
If you like me has a Horizontal ScrollView and want to scroll to end of it (in my case to most right of it), you need to change some parts of the accepted answer:
You can use the UIScrollView's
setContentOffset:animated:
function to scroll to any part of the content view. Here's some code that would scroll to the bottom, assuming your scrollView isself.scrollView
:Hope that helps!
Swift version of the accepted answer for easy copy pasting:
Simplest Solution:
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)
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):A Swift 2.2 solution, taking
contentInset
into accountThis should be in an extension
Note that you may want to check if
bottomOffset.y > 0
before scrollA swifty implementation:
use it:
Scroll To Top
Scroll To Bottom
What if
contentSize
is lower thanbounds
?For Swift it is:
I also found another useful way of doing this in the case you are using a UITableview (which is a subclass of UIScrollView):
Using UIScrollView's
setContentOffset:animated:
function to scroll to the bottom in Swift.With an (optional) footerView and contentInset, the solution is:
valdyr, hope this will help you:
Category to the rescue!
Add this to a shared utility header somewhere:
And then to that utility implementation:
Then Implement it wherever you like, for instance:
A good way to ensure the bottom of your content is visible is to use the formula:
This ensures the bottom edge of your content is always at or above the bottom edge of the view. The
MIN(0, ...)
is required becauseUITableView
(and probablyUIScrollView
) ensurescontentOffsetY >= 0
when the user tries to scroll by visibly snappingcontentOffsetY = 0
. This looks pretty weird to the user.The code to implement this is:
If you don't need animation, this works:
While
Matt
solution seems correct to me you need to take in account also the collection view inset if there is one that has been set-up.The adapted code will be:
In swift:
Solution to scroll to last item of a table View :
Swift 3 :
It looks like all of the answers here didn't take the safe area into consideration. Since iOS 11, iPhone X had a safe area introduced. This may affect the scrollView's
contentInset
.For iOS 11 and above, to properly scroll to the bottom with the content inset included. You should use
adjustedContentInset
instead ofcontentInset
. Check this code:contentOffset.x
):References:
Didn't work for me, when I tried to use it in
UITableViewController
onself.tableView
(iOS 4.1)
, after addingfooterView
. It scrolls out of the borders, showing black screen.Alternative solution:
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
.For Horizontal ScrollView
If you like me has a Horizontal ScrollView and want to scroll to end of it (in my case to most right of it), you need to change some parts of the accepted answer:
Objective-C
Swift
Xamarin.iOS version of accepted answer
Xamarin.iOS version for
UICollectionView
of the accepted answer for ease in copying and pasting