Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView not scrolling after switching to iOS 7

In iOS 6, I had a UITableView created using QuickDialog in my app. It scrolled normally. When I switched to iOS 7, the same UITableView does not scroll properly. I can drag to the bottom (the scroller compresses) but when I release, it pops back up the to the top. I've been playing with viewDidAppear to try and diagnose the problem. See the code block below.

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"Content height: %f",self.quickDialogTableView.contentSize.height);
    [self.quickDialogTableView reloadData];
    NSLog(@"Content height: %f",self.quickDialogTableView.contentSize.height);
    [self.quickDialogTableView layoutIfNeeded];
    NSLog(@"Content height: %f",self.quickDialogTableView.contentSize.height); 
 }

The output of this block in iOS 7 is

Content height: 0.000000
Content height: 836.000000
Content height: 0.000000

Meanwhile, the output of this block in iOS 6 (simulator) is

Content height: 836.000000
Content height: 836.000000
Content height: 836.000000

Also to try and diagnose the problem, I set up a button that would trigger [self.quickDialogTableView reloadData]. Whenever that button is pushed, the scrolling behavior begins to function normally. Then when I leave the view and come back, the scrolling fails again (until the button is pushed). To be clear, I have tried to put a reloadData in viewWillAppear by itself (i.e., removing the last two lines in the code block above) and it does not correct the scrolling.

I'm looking for clues as to where I might look to correct the issue. Thanks in advance for any help.

like image 868
Fausto Morales Avatar asked Sep 24 '13 04:09

Fausto Morales


3 Answers

Okay, so I couldn't figure out the source of the problem but I did find a workaround that I hope helps someone else. Or at least, maybe helps someone else point out what's really wrong.

I created a property called trueContentSize where I store what the correct size is.

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.quickDialogTableView reloadData]; // Calculates correct contentSize
    self.trueContentSize = self.quickDialogTableView.contentSize; 
}

Then, in -viewDidLayoutSubviews I correct the contentSize manually.

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.quickDialogTableView.contentSize = self.trueContentSize; 
}
like image 106
Fausto Morales Avatar answered Oct 22 '22 13:10

Fausto Morales


Just thought I might throw in my two cents here..

Same problem was happening to me. Table loads fine at first, but navigate to a different screen, come back, and the table view's contentSize is 0,0 and unscrollable. Fausto's workaround wasn't reliably working, either.

What turned out to be the case for me was just the act of referencing topLayoutGuide in -viewWillLayoutSubviews. Doing that causes all of the above symptoms. Try it out in a new project:

  1. Setup a table view controller inside a tab controller.
  2. Give it 30 rows with just static content.
  3. Run and you can scroll. Switch tabs, then back, and you can scroll.
  4. Add NSLog(@"%@", self.topLayoutGuide); in -viewWillLayoutSubviews
  5. Run again, you can scroll, but switch tabs, no more scrolling.

Weird. Sounds like an iOS bug, or you just shouldn't reference topLayoutGuide in that method. Removing any reference to that property will fix the issue.

Edit: As of earlier this year, Apple confirmed via a Radar report I made this is an iOS bug. Don't reference topLayoutGuide until it's been resolved (whenever that will be, heh). :)

like image 3
Stephen C Avatar answered Oct 22 '22 13:10

Stephen C


This is my current temporary fix. Anyone figure out the real fix?

@interface UITableView (Extension)
@end

@implementation UITableView (Extension)

- (void)setContentSize:(CGSize)contentSize {
  if (contentSize.height != 0) {
    [super setContentSize:contentSize];
    NSLog(@"set content height %f", contentSize.height);
  } else {
    NSLog(@"set content size zero");
  }
}

@end
like image 1
ajayjapan Avatar answered Oct 22 '22 14:10

ajayjapan