contentView not indenting in iOS 6 UITableViewCell prototype cell

后端 未结 6 2069
醉酒成梦
醉酒成梦 2020-12-02 05:35

I am configuring a custom UITableViewCell using a prototype cell in a Storyboard. However, all the UILabels (and other UI elements) do not seem to

6条回答
  •  青春惊慌失措
    2020-12-02 06:20

    An alternative to subclassing is to revise the constraints in cellForRowAtIndexPath.

    Embed all the content of the cell inside a container view. Then point the leading and trailing constraints to the cell.contentView rather than the table view cell.

      UIView *containerView = [cell viewWithTag:999];
      UIView *contentView = [cell contentView];
    
      //remove existing leading and trailing constraints
      for(NSLayoutConstraint *c in [cell constraints]){
        if(c.firstItem==containerView && (c.firstAttribute==NSLayoutAttributeLeading || c.firstAttribute==NSLayoutAttributeTrailing)){
          [cell removeConstraint:c];
        }
      }
    
      NSLayoutConstraint *trailing = [NSLayoutConstraint
                                     constraintWithItem:containerView
                                     attribute:NSLayoutAttributeTrailing
                                     relatedBy:NSLayoutRelationEqual
                                     toItem:contentView
                                     attribute:NSLayoutAttributeTrailing
                                     multiplier:1
                                     constant:0];
    
      NSLayoutConstraint *leading = [NSLayoutConstraint
                                     constraintWithItem:containerView
                                     attribute:NSLayoutAttributeLeading
                                     relatedBy:NSLayoutRelationEqual
                                     toItem:contentView
                                     attribute:NSLayoutAttributeLeading
                                     multiplier:1
                                     constant:0];
    
      [cell addConstraint:trailing];
      [cell addConstraint:leading];
    

提交回复
热议问题