Greg
Greg

Reputation: 34818

how to fix the margin adjustment code attached for a 'Grouped" UITableView?

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];
}

Upvotes: 0

Views: 1328

Answers (2)

adamsiton
adamsiton

Reputation: 3672

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

Upvotes: 1

sosborn
sosborn

Reputation: 14694

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.

Upvotes: 0

Related Questions