UITableView correcting scroll position after device rotation

后端 未结 6 804
予麋鹿
予麋鹿 2021-01-03 13:45

I\'m building something like a reader for a book. When the user rotates the phone I want to increase the font size. I\'m using a UITableView to display chunks o

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-03 13:52

    Use delegate method willRotateToInterfaceOrientation: to store the visible cell in an array then using the other delegate method of UITableView didRotateFromInterfaceOrientation: scroll to the visible index path that you stored earlier in the array. This is recommended and you don't have to rely on the inconsistent 0.2 seconds wait in a different thread to handle post rotate event.

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        // Save the visible row position
        visibleRows = [tableview indexPathsForVisibleRows];
    }
    
    -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        // Scroll to the saved position prior to screen rotate
        [tableView scrollToRowAtIndexPath:[visibleRows objectAtIndex:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }
    

提交回复
热议问题