Iphone UITableViewCell自动重新设置任务问题
我在Iphone UITableViewCell自动重新设置任务问题,iphone,ios,uitableview,device-orientation,autoresizingmask,Iphone,Ios,Uitableview,Device Orientation,Autoresizingmask,我在UITableViewCell的contentView中有3个UI元素,当设备旋转时,这些元素需要正确对齐。目前,我强制这些UI元素的框架如下: segmentedControl1.frame = CGRectMake(165, 15, 130, 40); segmentedControl2.frame = CGRectMake(435, 15, 130, 40); segmentedControl3.frame = CGRectMake(710, 15, 130, 40); 我想知道如何
UITableViewCell
的contentView
中有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;
- (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;
}
+-content view------------+
| ============ | ============ ============
| | A | B | | | A | B | | A | B |
| ============ | ============ ============
+-------------------------+
但这似乎不起作用。这些元素都重叠在一起,布局不协调
有什么建议吗
更新1
以下是“表视图”单元格当前在横向模式下的外观:
--------------------------------------------------------------------------
| ============ ============ ============ |
| | A | B | | A | B | | A | B | |
| ============ ============ ============ |
--------------------------------------------------------------------------
当设备旋转到纵向模式时,我想要的是:
-------------------------------------------------------
| ============ ============ ============ |
| | A | B | | A | B | | A | B | |
| ============ ============ ============ |
-------------------------------------------------------
更新2
这是我在CellForRowatineXpath中的代码,包含了@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;
请注意,它们的超级视图应具有以下属性:
view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
我有点困惑,你说你有一个工作的景观配置。我想说的是,将硬编码的
CGRectMake
数字与引用的autoresizingMask
值相结合,已经导致布局倾斜
尽管如此,我的建议是:不要对初始帧的宽度使用硬编码数字。相反,您应该基于cell.contentView.bounds.size.width
进行计算。在您的情况下,类似这样的情况:
segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
- (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;
}
+-content view------------+
| ============ | ============ ============
| | A | B | | | A | B | | A | B |
| ============ | ============ ============
+-------------------------+
我在iPad模拟器上试过,效果很好。请注意,您不必更改单元格或内容视图的自动ResizingMask
。我知道这与UIView
的正常工作方式不同,但除了表视图单元格是奇怪的野兽之外,我没有其他解释:-)
回到原始代码:根本问题在于,硬编码的数字假设内容视图具有屏幕的全宽,而实际上其初始宽度要小得多(在我的环境中,cell.contentView.bounds.size.width
初始宽度为320)。因此,初始布局如下所示:
segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
- (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;
}
+-content view------------+
| ============ | ============ ============
| | A | B | | | A | B | | A | B |
| ============ | ============ ============
+-------------------------+
如您所见,布局与内容视图的初始宽度不成比例。稍后将内容视图调整为正确宽度时,控件也会根据其初始宽度和水平分布比例调整大小。这会导致更不相称:-)这不起作用。。。很抱歉还有什么建议吗?我用一些图形更新了问题,以显示我在说什么……对不起,我的错。我复制了这些视图来测试我的解决方案,但它确实失败了。-我已经用一个有效且经过测试的版本更新了我的答案:)嘿@Wolfert,我采纳了你的建议,但似乎仍然不起作用。事实上,有了上述自动重设密钥值,segmentedControl2和segmentedControl3正在消失。。。我用我正在使用的实际代码更新了我的问题。我不知道为什么这对你不起作用。我用UIView测试了它,它100%有效。也许这与您的tableview或SegmentControl有关。很抱歉,我帮不了什么忙。谢谢@Wolfert!看起来我的表视图确实有问题。我会检查一下,看看能不能做点什么。