UITableView correcting scroll position after device rotation

后端 未结 6 698
予麋鹿
予麋鹿 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:58

    Since I couldn't get a good answer I'll answer myself. I've looked everywhere but couldn't find a way to do what I wanted so I just used the shouldAutorotateToInterfaceOrientation method to increase the size of font and then start a little thread that sleeps for 0.2 seconds and after that scrolls to the desired row. Thanks for your help.

    Edit: Use delegate method willRotateToInterfaceOrientation: to store the visible cell in an array then use the delegate method didRotateFromInterfaceOrientation: to scroll to the visible index path that you recorded in the array.

    - (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];
    }
    

提交回复
热议问题