简体   繁体   English

iOS自动版式动态按钮放置

[英]iOS Auto Layout Dynamic Button Placement

I have a prototype cell with two stacked labels followed by a view that contains up to three buttons. 我有一个带有两个堆叠标签的原型单元,后跟一个最多包含三个按钮的视图。 It is possible for 0, 1, 2 or 3 buttons to be visible for any given cell (based on web xml file). 0、1、2或3按钮对于任何给定单元格都是可见的(基于Web xml文件)。 I would like the layout to be as follows (X represents buttons) 我希望布局如下(X表示按钮)

3 Buttons 3个按钮

  |       |                   
  | X X X |         
  |       |

2 Buttons 2个按钮

  |       |                   
  | X   X |         
  |       |

1 Button 1个按钮

  |       |                   
  |   X   |         
  |       |

My table cell code sets the frame of each button depending on the total number of buttons in the cell. 我的表格单元格代码根据单元格中按钮的总数来设置每个按钮的框架。 However the frame does seem to work until the cell is loaded for the second time (ie scroll so the cell is out of range and then back to it). 但是,该框架似乎确实可以工作,直到第二次加载该单元为止(即滚动以使该单元超出范围然后再返回它)。 Last time this occurred it was due to layout constraints. 上一次发生这种情况是由于布局限制。 Question is, how do i set up the constraints in storyboard to allow for each button to be in on of many locations? 问题是,我如何在情节提要中设置约束以允许每个按钮位于多个位置? Currently each button is pinned to top, bottom and leading edge of the button view. 当前,每个按钮固定在按钮视图的顶部,底部和前沿。 I know the leading edge constraint is causing the issue, but not sure what to use instead as the storyboard forces it. 我知道前沿约束是造成此问题的原因,但不确定在情节提要中强制使用什么代替。 Or is there something I can do in my cellForRowAtIndexPath method? 还是在cellForRowAtIndexPath方法中可以做些什么? Here is relevant code from that method 这是该方法的相关代码

    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;

UPDATE I created a custom view to try and over come the layout issues. 更新我创建了一个自定义视图,以尝试解决布局问题。 The view just houses four buttons and places them on the layoutviews method. 该视图仅包含四个按钮,并将它们放在layoutviews方法上。 I added a view of the custom class to my prototype cell and the layout seems to work. 我在原型单元中添加了自定义类的视图,布局似乎可以正常工作。 However when I change directions in the scrollveiw (ie down then up, up then down) some of the cells get misaligned. 但是,当我在scrollveiw中更改方向时(即从上到下,从上到下),某些单元格未对齐。 See image below. 参见下图。

Image 图片

I'd use different prototypes for the three cell types and set the alignment up in the storyboard. 对于这三种单元格类型,我将使用不同的原型,并在情节提要中设置对齐方式。 You'd end up with very little code required. 您最终只需要很少的代码。 Just dequeue a cell of the appropriate type in cellForRowAtIndexPath: and set it up - no need to manipulate the frames directly. 只需在cellForRowAtIndexPath:中将适当类型的单元格出队并进行设置-无需直接操作框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM