[英]iOS Auto Layout Dynamic Button Placement
我有一个带有两个堆叠标签的原型单元,后跟一个最多包含三个按钮的视图。 0、1、2或3按钮对于任何给定单元格都是可见的(基于Web xml文件)。 我希望布局如下(X表示按钮)
3个按钮
| |
| X X X |
| |
2个按钮
| |
| X X |
| |
1个按钮
| |
| X |
| |
我的表格单元格代码根据单元格中按钮的总数来设置每个按钮的框架。 但是,该框架似乎确实可以工作,直到第二次加载该单元为止(即滚动以使该单元超出范围然后再返回它)。 上一次发生这种情况是由于布局限制。 问题是,我如何在情节提要中设置约束以允许每个按钮位于多个位置? 当前,每个按钮固定在按钮视图的顶部,底部和前沿。 我知道前沿约束是造成此问题的原因,但不确定在情节提要中强制使用什么代替。 还是在cellForRowAtIndexPath方法中可以做些什么? 这是该方法的相关代码
static NSString *CellIdentifier = @"localdiningTableCell";
mhgiLocalDiningCell *diningcell = (mhgiLocalDiningCell*)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (diningcell == nil) {
diningcell = [[mhgiLocalDiningCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
mhgiDiningXmlObject *object = [self.xmlParser.allItems objectAtIndex:[indexPath row]];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [[object Name] sizeWithFont:diningcell.nameLabel.font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect frame = CGRectMake (10, 10, 280.0f, labelSize.height);
diningcell.nameLabel.frame = frame;
diningcell.nameLabel.lineBreakMode = NSLineBreakByWordWrapping;
diningcell.nameLabel.numberOfLines = 0;
diningcell.nameLabel.text= [object Name];
CGFloat nextYVal = diningcell.nameLabel.frame.origin.y + diningcell.nameLabel.frame.size.height + 10.0f;
NSString *descrText = @"";
if (![[object Type] isEqualToString:@""]){
descrText = [descrText stringByAppendingString:[object Type]];
descrText = [descrText stringByAppendingString:@"\n"];
}
descrText = [descrText stringByAppendingString:[NSString stringWithFormat:@"%@, %@, %@", [object Street], [object City], [object State]]];
descrText = [descrText stringByAppendingString:@"\n"];
descrText = [descrText stringByAppendingString:[NSString stringWithFormat:@"%@ miles from the hotel", [object Distance]]];
labelSize = [descrText sizeWithFont:diningcell.typeLabel.font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
frame = CGRectMake (10, nextYVal, 280.0f, labelSize.height);
diningcell.typeLabel.frame = frame;
diningcell.typeLabel.lineBreakMode = NSLineBreakByWordWrapping;
diningcell.typeLabel.numberOfLines = 0;
diningcell.typeLabel.text = descrText;
nextYVal += diningcell.typeLabel.frame.origin.y + diningcell.typeLabel.frame.size.height + 10.0f;
diningcell.ussite= [object UrbanSpoon];
diningcell.website= [object Website];
diningcell.dirsite= [object Maps];
NSMutableArray* buttons = [[NSMutableArray alloc] init];
[buttons removeAllObjects];
if ([[object Website] isEqualToString:@""]){
[diningcell.websiteButton setHidden:TRUE];
}
else
{
[diningcell.websiteButton setHidden:FALSE];
[buttons addObject:diningcell.websiteButton];
}
if ([[object UrbanSpoon] isEqualToString:@""]){
[diningcell.usButton setHidden:TRUE];
}
else
{
[diningcell.usButton setHidden:FALSE];
[buttons addObject:diningcell.usButton];
}
if ([[object Maps] isEqualToString:@""]){
[diningcell.dirButton setHidden:TRUE];
}
else
{
[diningcell.dirButton setHidden:FALSE];
[buttons addObject:diningcell.dirButton];
}
if (buttons.count == 1){
[[buttons objectAtIndex:0] setCenter:CGPointMake(150.0f, 25.0f)];
}
else if (buttons.count == 2){
[[buttons objectAtIndex:0] setCenter:CGPointMake(75.0f, 25.0f)];
[[buttons objectAtIndex:1] setCenter:CGPointMake(225.0f, 25.0f)];
}
else if (buttons.count == 3){
[[buttons objectAtIndex:0] setCenter:CGPointMake(75.0f, 25.0f)];
[[buttons objectAtIndex:1] setCenter:CGPointMake(150.0f, 25.0f)];
[[buttons objectAtIndex:2] setCenter:CGPointMake(225.0f, 25.0f)];
}
return diningcell;
更新我创建了一个自定义视图,以尝试解决布局问题。 该视图仅包含四个按钮,并将它们放在layoutviews方法上。 我在原型单元中添加了自定义类的视图,布局似乎可以正常工作。 但是,当我在scrollveiw中更改方向时(即从上到下,从上到下),某些单元格未对齐。 参见下图。
对于这三种单元格类型,我将使用不同的原型,并在情节提要中设置对齐方式。 您最终只需要很少的代码。 只需在cellForRowAtIndexPath:中将适当类型的单元格出队并进行设置-无需直接操作框架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.