I have 3 UI elements in the contentView
of the UITableViewCell
that need to be aligned properly when the device rotates. Currently, I enforce the frames of these UI Elements like this:
segmentedControl1.frame = CGRectMake(165, 15, 130, 40);
segmentedControl2.frame = CGRectMake(435, 15, 130, 40);
segmentedControl3.frame = CGRectMake(710, 15, 130, 40);
I would like to know how I can use the autoresizingMask property of these elements to make them resize and not-overlap on one another when the device is rotated from Landscape to portrait (default is portrait, iPad only). I do not want to have custom frame positions for each orientation.
I tried something like this:
segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
But this doesn't seem to work. The elements are all overlapped and unhappily laid out.
Any suggestions?
UPDATE 1
Here's what the table view cell currently looks like in landscape mode:
--------------------------------------------------------------------------
| ============ ============ ============ |
| | A | B | | A | B | | A | B | |
| ============ ============ ============ |
--------------------------------------------------------------------------
And here's what I want when the device is rotated to a portrait mode:
-------------------------------------------------------
| ============ ============ ============ |
| | A | B | | A | B | | A | B | |
| ============ ============ ============ |
-------------------------------------------------------
UPDATE 2 Here's my code in cellForRowAtIndexPath incorporating suggestions from @Wolfert
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// Configure the cell...
segmentedControl1.frame = CGRectMake(165, 15, 180, 40);
segmentedControl1.tag = indexPath.row;
segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
[cell.contentView addSubview:segmentedControl1];
segmentedControl2.frame = CGRectMake(435, 15, 180, 40);
segmentedControl2.tag = indexPath.row;
segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[cell.contentView addSubview:segmentedControl2];
segmentedControl3.frame = CGRectMake(710, 15, 180, 40);
segmentedControl3.tag = indexPath.row;
segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[cell.contentView addSubview:segmentedControl3];
return cell;
}