我在UITableViewCell
的contentView
中有3个UI元素,需要在设备旋转时正确对齐。目前,我执行这样,这些UI元素的框架:UITableViewCell autoresizingmask问题
segmentedControl1.frame = CGRectMake(165, 15, 130, 40);
segmentedControl2.frame = CGRectMake(435, 15, 130, 40);
segmentedControl3.frame = CGRectMake(710, 15, 130, 40);
我想知道我该如何使用这些元素的autoresizingMask属性,使他们调整大小和彼此不重叠时,该设备从横向旋转到纵向(默认为纵向,仅iPad)。我不想为每个方向定制框架位置。
我想是这样的:
segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
但这似乎并没有工作。这些元素都是重叠的,并且不幸地布置。
有什么建议吗?
更新1
下面是表视图单元格目前看起来像在横向模式下:
--------------------------------------------------------------------------
| ============ ============ ============ |
| | A | B | | A | B | | A | B | |
| ============ ============ ============ |
--------------------------------------------------------------------------
这里就是我想要的,当设备被旋转到肖像模式:
-------------------------------------------------------
| ============ ============ ============ |
| | A | B | | A | B | | A | B | |
| ============ ============ ============ |
-------------------------------------------------------
UPDATE 2 这是我的代码在合并的cellForRowAtIndexPath从建议@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;
}
这不起作用......对不起!还有什么建议?我更新了一些图形的问题,以显示我在说什么... – Ravi 2011-12-21 22:40:53
对不起,我的错。我复制了观点来测试我的解决方案,但确实失败了。 - 我已经更新了我的答案,并带有工作和测试版本:) – Wolfert 2011-12-22 08:54:40
嘿@Wolfert,我整合了你的建议,但它似乎仍然不起作用。事实上,通过上面的autoresizingMask值,segmentedControl2和segmentedControl3正在消失......我用我正在使用的实际代码更新了我的问题。 – Ravi 2011-12-23 01:26:08