2011-12-19 91 views
1

我在UITableViewCellcontentView中有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; 
} 

回答

3

这应该做的伎俩:

segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; 
segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 

需要注意的是他们的超级观点应该有这样的属性:

view.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
+1

这不起作用......对不起!还有什么建议?我更新了一些图形的问题,以显示我在说什么... – Ravi 2011-12-21 22:40:53

+0

对不起,我的错。我复制了观点来测试我的解决方案,但确实失败了。 - 我已经更新了我的答案,并带有工作和测试版本:) – Wolfert 2011-12-22 08:54:40

+0

嘿@Wolfert,我整合了你的建议,但它似乎仍然不起作用。事实上,通过上面的autoresizingMask值,segmentedControl2和segmentedControl3正在消失......我用我正在使用的实际代码更新了我的问题。 – Ravi 2011-12-23 01:26:08

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 | 
| ============   | ============    ============ 
+-------------------------+ 

正如你所看到的,布局是不相称的内容视图的初始宽度。当内容视图后来调整到其正确的宽度时,控件的大小也会根据它们的初始宽度和水平分布进行调整。这会导致更多的异议:-)