Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automaticallyAdjustsScrollViewInsets not working

I've created an extremely simple demo app to test the functionality of automaticallyAdjustsScrollViewInsets, but the last cell of the tableView is covered by my tab bar.

My AppDelegate code:

UITabBarController *tabControl = [[UITabBarController alloc] init]; tabControl.tabBar.translucent = YES; testViewController *test = [[testViewController alloc] init]; [tabControl setViewControllers:@[test]];  [self.window setRootViewController:tabControl]; 

My testViewController (subclass of UITableViewController) Code:

- (void)viewDidLoad { [super viewDidLoad]; self.automaticallyAdjustsScrollViewInsets = YES; self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds]; self.tableView.dataSource = self; self.tableView.scrollIndicatorInsets = self.tableView.contentInset; //[self.view addSubview:self.tableView];  // Do any additional setup after loading the view. }  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 20; }  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""]; cell.textLabel.text = @"test"; return cell; } 

Is this a bug in iOS 7? If not, what did I do wrong?

like image 660
danielmhanover Avatar asked Jan 11 '14 23:01

danielmhanover


1 Answers

I think that automaticallyAdjustsScrollViewInsets only works when your controllers view is a UIScrollView (a table view is one).

You're problem seems to be that your controller's view is a regular UIView and your UITableView is just a subview, so you'll have to either:

  • Make the table view the "root" view.

  • Adjust insets manually:

    UIEdgeInsets insets = UIEdgeInsetsMake(controller.topLayoutGuide.length,                                        0.0,                                        controller.bottomLayoutGuide.length,                                        0.0); scrollView.contentInset = insets; 

Edit:

Seems like the SDK is capable of adjusting some scroll views despite not being the controller's root view.

So far It works with UIScrollView's and UIWebView's scrollView when they are the subview at index 0.

Anyway this may change in future iOS releases, so you're safer adjusting insets yourself.

like image 83
Rivera Avatar answered Oct 11 '22 00:10

Rivera