Search code examples
uitableviewpositionrowtvosvertical-scrolling

Make tvOS tableview row stay in fixed position while scrolling


I am looking to reproduce the scrolling behavior of Netflix's tvOS app with a UITableView.

When you scroll through items of the app on Netflix, the focused row and the upcoming one are always in the same position.

With the default behavior of UITableView the focused row's position is different depending on the row.

How could I achieve this?


Solution

  • The UIScrollViewDelegate method scrollViewWillEndDragging:withVelocity:targetContentOffset: will allow you to control what content offset the focus engine sets on your scroll view: if you adjust the value that the targetContentOffset parameter points to, then the focus engine will scroll there.

    So you should implement that method, and use whatever method you like to calculate what the correct offset should be, and then set the value in the pointer:

    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
    {
        CGPoint defaultOffset = *targetContentOffset;
        CGPoint newOffset = myFunctionThatAdjustsTheOffset(defaultOffset);
        *targetContentOffset = newOffset;
    }