【问题标题】:Update UITableView size based on UIView constraints根据 UIView 约束更新 UITableView 大小
【发布时间】:2014-11-24 13:04:57
【问题描述】:

我有一个 UIView,里面有一个 UITableView。

在 UIViewController 中,我像这样实例化 UIView:

viewMain = [ViewMain alloc]initwithframe:cgrectmake(0,0,200,500)];

在这个视图中,在方法 initWithFrame:(CGRect)frame 中,我像这样实例化 UITableView:

_tableView = [UITableView alloc]iniWithFrame:CGRectMake(0,0,frame.size.width,frame.size.height)];

问题是当我对 viewMain 应用约束时,如何根据 viewMain 布局约束更新 tableView 的大小?

【问题讨论】:

    标签: ios nslayoutconstraint


    【解决方案1】:

    您需要向子视图添加约束(即,如果父视图发生更改,则自动布局本身),如下所示:

    NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:viewMain attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_tableView attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
    [_tableView addConstraint:left];
    
    NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:viewMain attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_tableView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
    [_tableView addConstraint:top];
    
    NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:viewMain attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_tableView attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
    [_tableView addConstraint:width];
    
    NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:viewMain attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_tableView attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
    [_tableView addConstraint:height];
    

    不确定您是否还需要这样做:

    _tableView.translatesAutoresizingMaskIntoConstraints = NO;
    

    在添加约束之前。

    【讨论】:

      【解决方案2】:

      如果您使用框架设置表格视图,那么您将无法使用约束更新布局。如果您想根据父视图更新表格视图,请使用情节提要中的约束来更新约束。

      【讨论】:

        【解决方案3】:

        基本上你必须在-tableView:cellForRowAtIndexPath:中的单元格上调用-setNeedUpdateConstraints-updateConstraintsIfNeeded

        this answer中查看非常详细的解释和示例代码。

        这是一个很棒的Tutorial by Ray Wenderlich

        你也可以在你的 tableViewController 中试试这个:

        - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
            if (IS_IOS8_OR_ABOVE) {
                return UITableViewAutomaticDimension;
            }
        
            //self.prototypeCell.bounds = CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), CGRectGetHeight(self.prototypeCell.bounds));
        
            [self configureCell:self.prototypeCell forRowAtIndexPath:indexPath];
        
            [self.prototypeCell updateConstraintsIfNeeded];
            [self.prototypeCell layoutIfNeeded];
        
            return [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
        }
        

        【讨论】:

          【解决方案4】:

          我会将 tableView 的 frame 设置为 mainView 的 frame。 即:

          _tableView = [UITableView alloc]iniWithFrame:CGRectMake(mainView.frame.origin.x,mainView.frame.origin.y,mainView.frame.size.width,mainView.frame.size.height)];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-04-23
            • 2017-12-24
            • 1970-01-01
            相关资源
            最近更新 更多