0

Trying to achieve the look & feel of a Grouped TableView, with only one item per section, and but with hardly any margins (gives me rounded edged view with the ability for the user to be able to choose the color they want).

I have it working, HOWEVER when the user changes orientation I had to use the didRotateFromInterfaceOrientation method (as willRotateToInterfaceOrientation didn't work), BUT the effect is that you do see the margins change quickly in that fraction of a second after the tableView displays.

QUESTION - Any way to fix things so one doesn't see this transition?

- (void) removeMargins {
  CGFloat marginAdjustment = 7.0;
  CGRect f = CGRectMake(-marginAdjustment, 0, self.tableView.frame.size.width + (2 * marginAdjustment), self.tableView.frame.size.height);
  self.tableView.frame = f;
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  [self removeMargins];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
  [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  [self removeMargins];
}
Greg
  • 34,042
  • 79
  • 253
  • 454

2 Answers2

1

I Think the problem is that in willRotateToInterfaceOrientation the frame of the tableView hasn't resized yet, so your frame calculations are incorrect. on didRotateFromInterfaceOrientation the frame has already changed.

I think the easiest way to solve this is to subclass UITableView and override layoutSubviews. This method is called every time the frame of the view changes in a way that may require it's subviews to change.

The following code worked for me without an animation glitch:

@interface MyTableView : UITableView {
}

@end

@implementation MyTableView

-(void) layoutSubviews
{
    [super layoutSubviews];

    CGFloat marginAdjustment = 7.0;

    if (self.frame.origin.x != -marginAdjustment) // Setting the frame property without this check will call layoutSubviews again and cause a loop
    {
        CGRect f = CGRectMake(-marginAdjustment, 0, self.frame.size.width + (2 * marginAdjustment), self.frame.size.height);
        self.frame = f;
    }
}

@end
adamsiton
  • 3,642
  • 32
  • 34
  • thanks - so does layoutSubviews occur after any rotation? i.e. the code would work in a non-rotation, or after any type of rotation? – Greg Oct 11 '11 at 22:07
  • Exactly. Every time drawRect is called, or there is a need to recalculate the frames of the subviews. And in UITableView, the subviews need to change every time that the frame of the table changes - specifically, every rotate. (if the table resize mask is set of course). – adamsiton Oct 11 '11 at 22:54
  • thanks - as an aside it seems like this issue with margins only occurs for the GROUPED mode of a UITableView. Doesn't seem to occur in the normal mode? Just the way apple implemented the control I guess? – Greg Oct 11 '11 at 23:43
0

You say that willRotateToInterfaceOrientation didn't work, but you didn't say why.

If the reason is that willRotateToInterfaceOrientation isn't being called, then please take a look at this question.

If you get this working then I believe your other question will solve itself.

Community
  • 1
  • 1
sosborn
  • 14,676
  • 2
  • 42
  • 46
  • did put in a NSLog line & I do see that willRotateToInterfaceOrientation is being called - but I think you're right in that the other question will solve it... – Greg Oct 11 '11 at 05:45
  • re why willRotateToInterfaceOrientation didn't work - it just didn't remove the margins at all (even though I ran the same margin removal code - per my question) – Greg Oct 11 '11 at 05:45