【问题标题】:Swift - UITableViewCell class contentView programmatically constraints doesn't have widthSwift - UITableViewCell 类 contentView 以编程方式约束没有宽度
【发布时间】:2016-02-19 04:51:09
【问题描述】:
class Cell:UITableViewCell {
   var myImage:UIImageView = UIImageView()
   override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
        myImage.setTranslatesAutoresizingMaskIntoConstraints(false)
        time.setTranslatesAutoresizingMaskIntoConstraints(false)
        contentView.addSubview(userImage)
        contentView.addSubview(time)
        let viewsDict = ["contentView":contentView,"myImage":myImage,"time":time]
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[contentView(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-30-[myImage(50)]-[time]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-25-[myImage(50)]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
   }
}

我希望将时间附加在 contentView 的子视图的右侧,但它在 myImage 旁边: 即使在横向模式下,contentView 也不会到达所有屏幕:

我已尝试使用contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[contentView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict)),但收到错误消息:

The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x79e5d7c0 H:|-(0)-[UITableViewCellContentView:0x79e59f70]   (Names: '|':myApp.Cell:0x79e58f00'cell' )>
        When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
    2015-01-13 20:05:11.546 myApp[6117:775684] View hierarchy unprepared for constraint.
        Constraint: <NSLayoutConstraint:0x79e5d7c0 H:|-(0)-[UITableViewCellContentView:0x79e59f70]   (Names: '|':myApp.Cell:0x79e58f00'cell' )>
        Container hierarchy: 
    <UITableViewCellContentView: 0x79e59f70; frame = (0 0; 320 44); gestureRecognizers = <NSArray: 0x79e5a8a0>; layer = <CALayer: 0x79e5a140>>
       | <UILabel: 0x79e59520; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x79e595e0>>
       | <UIImageView: 0x79e59750; frame = (0 0; 0 0); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x79e597d0>>
        View not found in container hierarchy: <myApp.Cell: 0x79e58f00; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x79e59b50>>
        That view's superview: NO SUPERVIEW
    2015-01-13 20:05:11.857 myApp[6117:775684] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view.  Does the constraint reference something from outside the subtree of the view?  That's illegal. constraint:<NSLayoutConstraint:0x79e5d7c0 H:|-(0)-[UITableViewCellContentView:0x79e59f70]   (Names: '|':myApp.Cell:0x79e58f00'cell' )> view:<UITableViewCellContentView: 0x79e59f70; frame = (0 0; 320 44); gestureRecognizers = <NSArray: 0x79e5a8a0>; layer = <CALayer: 0x79e5a140>>'

更新:我在向 tableView 添加约束时修复了行的宽度,但标签 time 仍然不在 contentView 的右侧。有什么建议么?看起来 contentView 不像 tableView 宽度那样长。我不知道...

【问题讨论】:

  • 您只需要更改文本对齐方式。

标签: ios uitableview swift constraints


【解决方案1】:

尝试在 superview 上添加 contentView 约束:

contentView.superview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[contentView(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))

您可能还需要调用它以使单元格正确呈现:

self.setTranslatesAutoresizingMaskIntoConstraints(false)

【讨论】:

  • self.setTranslatesAutoresizingMaskIntoConstraints(false) 将其放入具有信息的 Cell 分类单元格中。 contentView.superview?.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[contentView(500)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict)) 这一行对单元格没有任何作用
  • 当你将 contentView 约束添加到 superview 时,你是否也删除了将它添加到 contentView 的行?您是否仍然遇到同样的错误?
  • 是的,但 .superview 没有做任何事情,它必须是 contentView.addConstraints....也许这是我不知道的错误..
  • contentView.superview?.addSubview(timeLabel) contentView.superview?.addConstraints(...) 是答案:)
【解决方案2】:

您可以将标签文本对齐设置为右对齐,然后标签将对齐到 contentView 的右侧。像这样。

time.textAlignment = NSTextAlignment.Right

【讨论】:

    【解决方案3】:

    我建议对您的约束使用以下设置(从 viewDict 行开始替换您的代码):

    let viewsDict = ["contentView":contentView,"myImage":myImage,"time":time]
    
    self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[contentView(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
    self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[-10-contentView-10-]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
    
    contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-30-[myImage(50)]-(>=10)-[time]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
    contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-25-[myImage(50)]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
    
    self.setTranslatesAutoresizingMaskIntoConstraints(false)
    contentView.setTranslatesAutoresizingMaskIntoConstraints(false)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      相关资源
      最近更新 更多