我在contentView
的{{1}}中有3个UI元素需要在设备旋转时正确对齐。目前,我强制执行这些UI元素的框架:
UITableViewCell
我想知道如何使用这些元素的autoresizingMask属性,当设备从横向旋转到纵向时(默认为纵向,仅限iPad),可以使它们调整大小并且不重叠。我不希望每个方向都有自定义框架位置。
我试过这样的事情:
segmentedControl1.frame = CGRectMake(165, 15, 130, 40);
segmentedControl2.frame = CGRectMake(435, 15, 130, 40);
segmentedControl3.frame = CGRectMake(710, 15, 130, 40);
但这似乎不起作用。这些元素都是重叠的,并且是不幸的。
有什么建议吗?
更新1
以下是横向模式下表格视图单元格目前的样子:
segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
当设备旋转到纵向模式时,这就是我想要的:
--------------------------------------------------------------------------
| ============ ============ ============ |
| | A | B | | A | B | | A | B | |
| ============ ============ ============ |
--------------------------------------------------------------------------
更新2 这是我在cellForRowAtIndexPath中的代码,其中包含来自@Wolfert的建议
-------------------------------------------------------
| ============ ============ ============ |
| | A | B | | A | B | | A | B | |
| ============ ============ ============ |
-------------------------------------------------------
答案 0 :(得分:3)
这应该可以解决问题:
segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
请注意,他们的超级视图应该具有以下属性:
view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
答案 1 :(得分:1)
我说你有一个有效的横向配置我有点困惑。我会说,将您的硬编码CGRectMake
数字与您引用的autoresizingMask
值组合在一起会导致布局偏斜。
尽管如此,这是我的建议:你不应该使用硬编码的数字作为初始帧的宽度。相反,您应该将计算基于cell.contentView.bounds.size.width
。在你的情况下,像这样:
- (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];
NSArray* items = [NSArray arrayWithObjects:@"A", @"B", nil];
UISegmentedControl* segmentedControl1 = [[[UISegmentedControl alloc] initWithItems:items] autorelease];
UISegmentedControl* segmentedControl2 = [[[UISegmentedControl alloc] initWithItems:items] autorelease];
UISegmentedControl* segmentedControl3 = [[[UISegmentedControl alloc] initWithItems:items] autorelease];
[cell.contentView addSubview:segmentedControl1];
[cell.contentView addSubview:segmentedControl2];
[cell.contentView addSubview:segmentedControl3];
// Some values I like
CGFloat marginHorizontal = 10;
CGFloat spacingHorizontal = 8;
// This is the crucial line!
CGFloat totalWidth = cell.contentView.bounds.size.width;
// That's how much is available to the three controls
CGFloat availableWidth = totalWidth - 2 * marginHorizontal - 2 * spacingHorizontal;
// That's how much each control gets - initially!
CGFloat segmentedControlWidth = availableWidth / 3.0;
// These are the numbers from your example. With these numbers your table view
// delegate probably implements tableView:heightForRowAtIndexPath:()
CGFloat marginVertical = 15;
CGFloat segmentedControlHeight = 40;
CGRect segmentedControlFrame = CGRectMake(marginHorizontal,
marginVertical,
segmentedControlWidth,
segmentedControlHeight);
segmentedControl1.frame = segmentedControlFrame;
segmentedControlFrame.origin.x += spacingHorizontal + segmentedControlWidth;
segmentedControl2.frame = segmentedControlFrame;
segmentedControlFrame.origin.x += spacingHorizontal + segmentedControlWidth;
segmentedControl3.frame = segmentedControlFrame;
// These are the values you cited initially, they are fine
segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
return cell;
}
我在iPad模拟器上尝试了这个,它运行得很好。请注意,您不必更改单元格或内容视图的autoresizingMask
。我知道这与UIView
通常的工作原理有所不同,但我没有解释,除了表格视图是奇怪的野兽: - )
回到原始代码:根本问题是硬编码数字假设内容视图具有屏幕的整个宽度,而实际上它的初始宽度要小得多(在我的环境中cell.contentView.bounds.size.width
最初是320)。因此,初始布局看起来像这样:
+-content view------------+
| ============ | ============ ============
| | A | B | | | A | B | | A | B |
| ============ | ============ ============
+-------------------------+
如您所见,布局与内容视图的初始宽度不成比例。当内容视图稍后调整到其正确宽度时,控件也会按其初始宽度和水平分布的比例调整大小。这导致更多不合比例的: - )