I tried to read the contentOffset of the view table after rotation in the method
willRotateToInterfaceOrientation: Duration :. I created a sample using a UITableViewController with a search bar in tableHeaderView.
Scenario 1: This device is in "Portrait" mode, and I hide the search bar. Then I rotate the device to the landscape. After that, I would expect NOT to see that UISearchbar and contentOffset remain the same.
Scenario 2: This device is in the Landscape, and I hide the search bar. Then I rotate the device to the portrait. After that, I would expect NOT to see the UISearchbar and contentOffset remain the same.
Scenario 1 works as expected. Scenario 2 pulls out the search string and content-content - zero
Does anyone know why ContentOffset matters Zero? I expect this to be 44 (search bar height).
Is there any way to solve this? How do you do this?
// // ViewController.m // test // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; } - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ //is necessary to prevent showing searchbar dispatch_async(dispatch_get_main_queue(), ^{ double y = self.tableView.contentOffset.y; self.tableView.contentInset = UIEdgeInsetsMake(-1*y, 0, 0, 0); NSLog(@"y %f",y); NSLog(@"Begin offset %@",NSStringFromCGPoint(self.tableView.contentOffset)); }); } - (void)viewDidAppear:(BOOL)animated{ self.tableView.tableHeaderView = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44)]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { #warning Potentially incomplete method implementation. // Return the number of sections. return 0; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation. // Return the number of rows in the section. return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell... return cell; } #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } @end
source share