iOS UIScrollView/UITableView contentOffset gets re

2019-07-21 12:13发布

问题:

I have a simple UIViewController with a UITableView as its view's subview. I want to have the option to hide the tableViewHeader of my tableview. This is working fine - I can adjust the content offset with the height of the headerView and its all good. However, when I am to transition to a new VC (by pushing) - during the transition animation the tableView's contentOffset gets reset to {0,0} - essentially showing the header view.

Is there a way I can keep the contentOffset of the tableView until the new VC has been shown on screen ?

I noticed that if the contentSize of the tableView is larger than the superview's bounds (i.e. there are more cells than the frame can display) - then the contentOffset does not get reset. I have a log of self.tableView.contentOffset.y at viewDidDisappear - in the first instance(when the contentSize is smaller) - the log prints 0, if that is not the case - then it prints 44 (which is the expected value). ?

UPDATE:

Setting the required contentOffset values in viewWillLayoutSubviews or in viewDidLayoutSubviews seems to work fine although I do not understand the implications of doing it like this. Unfortunately this does not work on iOS 7, and the expected behaviour is achieved only on iOS 8.