gpt4 book ai didi

iphone - UITableViewCell 自动调整大小掩码问题

转载 作者:行者123 更新时间:2023-12-03 20:45:15 27 4
gpt4 key购买 nike

我在 UITableViewCellcontentView 中有 3 个 UI 元素,需要在设备旋转时正确对齐。目前,我强制执行这些 UI 元素的框架,如下所示:

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

这是表格 View 单元格当前在横向模式下的样子:

--------------------------------------------------------------------------
| ============ ============ ============ |
| | A | B | | A | B | | A | B | |
| ============ ============ ============ |
--------------------------------------------------------------------------

这就是当设备旋转到纵向模式时我想要的:

-------------------------------------------------------
| ============ ============ ============ |
| | A | B | | A | B | | A | B | |
| ============ ============ ============ |
-------------------------------------------------------

更新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;
}

最佳答案

这应该可以解决问题:

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

请注意,他们的 super View 应该具有以下属性:

view.autoresizingMask = UIViewAutoresizingFlexibleWidth;

关于iphone - UITableViewCell 自动调整大小掩码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8559507/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com