I have a custom UIView that I set for the tableHeaderView property of the UITableView during loadView:
headerView = [[MYViewClass alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 120)]; [headerView sizeToFit]; self.tableView.tableHeaderView = headerView;
The view is not drawn correctly, with part of the cropped and white space above the table. When the device rotates, the view almost disappears. When you turn back, the view is now larger than the heading space and hides some cells in the table.
To troubleshoot, I overridden the setFrame method in my custom view class:
- (void) setFrame:(CGRect)frame { [super setFrame:frame]; NSLog(@"%@ - %@", NSStringFromSelector(_cmd), NSStringFromCGRect(frame)); }
I also set a breakpoint in the NSLog statement so that I can see what is called setFrame, and get some odd results that I cannot explain, and hope someone can shed light on what is happening and why.
During loadView
1. initWithFrame calls: setFrame: - {{0, 0}, {320, 120}} 2. sizeToFit calls: setFrame: - {{0, 0}, {320, 109}} 3. setTableHeaderView calls: setFrame: - {{0, 0}, {320, 109}} 4. _adjustTableHeaderAndFooterViews calls: setFrame: - {{0, 0}, {320, 109}} 5. _resizeWithOldSuperviewSize calls: setFrame: - {{0, 0}, {320, 65}} 6. _adjustTableHeaderAndFooterViews calls: setFrame: - {{0, 0}, {320, 65}} 7. _adjustTableHeaderAndFooterViews calls: setFrame: - {{0, 0}, {320, 65}}
Rotate device left
1. _resizeWithOldSuperviewSize calls: setFrame: - {{0, 0}, {480, 0}} 2. _adjustTableHeaderAndFooterViews calls: setFrame: - {{0, 0}, {480, 0}} 3. _resizeWithOldSuperviewSize calls: setFrame: - {{0, 0}, {480, 12}} 4. _adjustTableHeaderAndFooterViews calls: setFrame: - {{0, 0}, {480, 12}}
Rotate device to the right
1. _resizeWithOldSuperviewSize calls: setFrame: - {{0, 0}, {320, 172}} 2. _adjustTableHeaderAndFooterViews calls: setFrame: - {{0, 0}, {320, 172}} 3. _resizeWithOldSuperviewSize calls: setFrame: - {{0, 0}, {320, 160}} 4. _adjustTableHeaderAndFooterViews calls: setFrame: - {{0, 0}, {320, 160}}
This explains why my gaze first looks truncated, and then almost disappears and finally ends with overlapping cells. It seems that _resizeWithOldSuperviewSize is the culprit, and I don't understand why it is being called and where it gets these odd values from.
I have a very difficult job calling [self.tableView.tableHeaderView sizeToFit] in viewDidAppear: and didRotateFromInterfaceOrientation: which will return the frame to the correct size, but the redrawing is terrible as both happen after the view is visible or after the rotation animation, Trying to set this to any time before the visible view calls _resizeWithOldSuperviewSize to return the frame to these odd sizes.