views:

2957

answers:

4

hello, how can I make the uiscrollview scroll to the bottom within my code? or in a more generic way to any point of a subview? thanks.

+10  A: 

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:

CGPoint bottomOffset = CGPointMake(0, [scrollView contentSize].height);
[scrollView setContentOffset: bottomOffset animated: YES];

Hope that helps!

Ben Gotow
thanks! that helped.
nico
+1  A: 

I also found another useful way of doing this in the case you are using a UITableview (which is a subclass of UIScrollView):

[(UITableView *)self.view scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

nico
A: 

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.

Alternative solution:

 CGFloat height = self.tableView.contentSize.height; 

 [self.tableView setTableFooterView: myFooterView];
 [self.tableView reloadData];

 CGFloat delta = self.tableView.contentSize.height - height;
 CGPoint offset = [self.tableView contentOffset];
 offset.y += delta;

 [self.tableView setContentOffset: offset animated: YES];
valdyr
A: 

valdyr, hope this will help you:

CGPoint bottomOffset = CGPointMake(0, [textView contentSize].height - textView.frame.size.height);

if (bottomOffset.y > 0) [textView setContentOffset: bottomOffset animated: YES];

Misha