What is the best way to scroll to a focused item?

I've some buttons outside my screen, I'd like to focus them

and I'd like to see them when they are focused.


I tried to put them in a UIScrollView, that does scroll

but sometimes it's not what I expected to get: I can't see my item at all!


Any idea?

Accepted Reply

I think I see the issue: the focus engine will scroll your scroll view to the maximum offset if you focus on the last item in the scroll view. This is intentional behavior, because without this it could sometimes be impossible to see the very top or very bottom of a scroll view.


Your best option is probably to choose a different content size for your scroll view: it should be large enough to contain your buttons, but not so large that scrolling to the bottom of it would put your buttons off-screen.


If you can't change the content size of your scroll view, you may instead want to use the UIScrollViewDelegate method to help the focus engine choose a better offset by implementing this method:


- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);


The targetContentOffset parameter is a pointer to a CGPoint that is the content offset the focus engine is about to scroll your scroll view to. If you modify that point, then the focus engine will scroll there instead. You can use this to make sure it never scrolls too far past your buttons.

Replies

The focus engine should automatically choose a content offset for the scroll view that brings your view on-screen when that view becomes focused. Can you provide some more details about how your buttons and scroll view are set up? Such as, what is the size and the contentSize of your scroll view, how big are your buttons and where are they positioned within the scroll view, etc.

Thanks for your answer, here are some details.

I've a UIScrollView with a unique UIImageView inside.

Frame of the UIScrollView is 0,0,1920,1080

Frame of the UIImageView is 0,0,6000,4056

ContentOffset of the UIScrollView is 6000,4056


I've 10 UIButtons in this UIImageViewn, their coordinates are:

x:607.6 y:100.0

x:760.8 y:300.0

x:430.1 y:500.0

x:511.6 y:700.0

x:698.6 y:900.0

x:220.7 y:1100.0

x:232.1 y:1300.0

x:517.8 y:1500.0

x:602.7 y:1700.0

x:204.6 y:1900.0

and their size are all the same : 167,328


When focus goes to last button (204,1900), the scrollview scrolls to the bottom of the uiimageview,

and I can't see the last button at all.


My UIButtons are created programmatically, with this code:


m_butTest[i] = [UIButton buttonWithType:UIButtonTypeCustom];

[m_butTest[i] setBackgroundImage:[UIImage imageNamed:@"off"] forState:UIControlStateNormal];

[m_butTest[i] setBackgroundImage:[UIImage imageNamed:@"on"] forState:UIControlStateFocused];

[m_img addSubview:m_butTest[i]];

m_butTest[i].frame=CGRectMake(X,Y,167, 328);


Hope this helps to understand what's happening here.


It's still the same behavior in beta 2.

I think I see the issue: the focus engine will scroll your scroll view to the maximum offset if you focus on the last item in the scroll view. This is intentional behavior, because without this it could sometimes be impossible to see the very top or very bottom of a scroll view.


Your best option is probably to choose a different content size for your scroll view: it should be large enough to contain your buttons, but not so large that scrolling to the bottom of it would put your buttons off-screen.


If you can't change the content size of your scroll view, you may instead want to use the UIScrollViewDelegate method to help the focus engine choose a better offset by implementing this method:


- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);


The targetContentOffset parameter is a pointer to a CGPoint that is the content offset the focus engine is about to scroll your scroll view to. If you modify that point, then the focus engine will scroll there instead. You can use this to make sure it never scrolls too far past your buttons.

Thanks a lot for your answer!

Thanks! (A couple months later, but this is still the answer I was looking for.)