iOS以编程方式为表视图单元格内容创建NSLayoutConstraint

 逃跑的骨拉拉gf_761 发布于 2023-02-13 15:30

我想在cellForRowAtIndexPath中添加一些视图到我的单元格内容视图和它们的约束但没有任何作用.我有这样的事情:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:imageView
                                  attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeLeft multiplier:1.0f constant:10.0f];

[cell.contentView addConstraint:constraint];

我该怎么做?

1 个回答
  • 几点意见:

      您创建此特定约束是正确的.显然,您不能只设置左约束,而是需要指定将明确定义frame单元格子视图的所有约束.例如,不仅要定义左(或前导)约束,还要定义顶部,底部和宽度约束.或者定义左约束加上宽度和高度约束,并指定垂直y约束.有很多不同的方法可以做到这一点,但关键是你必须添加所有明确定义frame所有子视图的约束.

      例如,您可能具有以下内容:

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString *cellIdentifier = @"Cell";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
      
          UIImageView *customImageView;
          UILabel *customLabel;
      
          if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
      
              customImageView = [[UIImageView alloc] init];
              customImageView.translatesAutoresizingMaskIntoConstraints = NO;
              customImageView.tag = IMAGEVIEWTAG;
              [cell.contentView addSubview:customImageView];
      
              [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customImageView
                                                                           attribute:NSLayoutAttributeLeading
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:cell.contentView
                                                                           attribute:NSLayoutAttributeLeft
                                                                          multiplier:1.0
                                                                            constant:25.0]];
      
              [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customImageView
                                                                           attribute:NSLayoutAttributeWidth
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:nil
                                                                           attribute:NSLayoutAttributeNotAnAttribute
                                                                          multiplier:1.0
                                                                            constant:30.0]];
      
              [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customImageView
                                                                           attribute:NSLayoutAttributeTop
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:cell.contentView
                                                                           attribute:NSLayoutAttributeTop
                                                                          multiplier:1.0
                                                                            constant:3.0]];
      
              [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customImageView
                                                                           attribute:NSLayoutAttributeBottom
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:cell.contentView
                                                                           attribute:NSLayoutAttributeBottom
                                                                          multiplier:1.0
                                                                            constant:-3.0]];
      
              customLabel = [[UILabel alloc] init];
              customLabel.translatesAutoresizingMaskIntoConstraints = NO;
              customLabel.tag = LABELTAG;
              [cell.contentView addSubview:customLabel];
      
              [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customLabel
                                                                           attribute:NSLayoutAttributeLeading
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:customImageView
                                                                           attribute:NSLayoutAttributeTrailing
                                                                          multiplier:1.0
                                                                            constant:10.0]];
      
              [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customLabel
                                                                           attribute:NSLayoutAttributeTrailing
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:cell.contentView
                                                                           attribute:NSLayoutAttributeTrailing
                                                                          multiplier:1.0
                                                                            constant:-10.0]];
      
              [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customLabel
                                                                           attribute:NSLayoutAttributeTop
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:cell.contentView
                                                                           attribute:NSLayoutAttributeTop
                                                                          multiplier:1.0
                                                                            constant:3.0]];
      
              [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customLabel
                                                                           attribute:NSLayoutAttributeBottom
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:cell.contentView
                                                                           attribute:NSLayoutAttributeBottom
                                                                          multiplier:1.0
                                                                            constant:-3.0]];
      
          }
          else {
              customImageView = (id)[cell.contentView viewWithTag:IMAGEVIEWTAG];
              customLabel     = (id)[cell.contentView viewWithTag:LABELTAG];
          }
      
          customImageView.image = ...;
          customLabel.text      = ...;
      
          return cell;
      }
      

      显然,您经常使用UITableViewCell子类来促进跟踪自定义控件的过程,但我希望保持示例简单.

      如果您不确定是否已明确定义约束,请运行应用程序,并在呈现UI后,暂停应用程序并在(lldb)提示符处输入以下内容:

      po [[UIWindow keyWindow] _autolayoutTrace]
      

      如果任何视图被明确定义(即是否缺少任何约束),这将通知您.

      如果要查看frame所有视图的内容,可以在(lldb)提示符处输入以下内容:

      po [[UIWindow keyWindow] recursiveDescription]
      

      确保指定translatesAutoresizingMaskIntoConstraintsNO所有子视图,如我上面的代码示例中一样.

      虽然您可以使用constraintWithItem常用的人来定义约束,但通常constraintsWithVisualFormat可以更简洁地定义约束.将上面的代码示例与此代码示例进行对比:

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString *cellIdentifier = @"Cell";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
      
          UIImageView *customImageView;
          UILabel *customLabel;
      
          if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
      
              customImageView = [[UIImageView alloc] init];
              customImageView.translatesAutoresizingMaskIntoConstraints = NO;
              customImageView.tag = IMAGEVIEWTAG;
              [cell.contentView addSubview:customImageView];
      
              customLabel = [[UILabel alloc] init];
              customLabel.translatesAutoresizingMaskIntoConstraints = NO;
              customLabel.tag = LABELTAG;
              [cell.contentView addSubview:customLabel];
      
              NSDictionary *views = NSDictionaryOfVariableBindings(customImageView, customLabel);
              [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-25-[customImageView(30)]-[customLabel]|" options:0 metrics:nil views:views]];
              [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-3-[customImageView]-3-|"                 options:0 metrics:nil views:views]];
              [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-3-[customLabel]-3-|"                     options:0 metrics:nil views:views]];
          }
          else {
              customImageView = (id)[cell.contentView viewWithTag:IMAGEVIEWTAG];
              customLabel     = (id)[cell.contentView viewWithTag:LABELTAG];
          }
      
          customImageView.image = ...;
          customLabel.text      = ...;
      
          return cell;
      }
      

    2023-02-13 15:33 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有