Swift - UITableViewCell class contentView 编程约束没有宽度

Swift - UITableViewCell class contentView programmatically constraints doesn't have width

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 宽度那样长。我不知道...

尝试在父视图上添加 contentView 约束:

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

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

self.setTranslatesAutoresizingMaskIntoConstraints(false)

您可以将标签文本对齐方式设置为右对齐,那么标签将与contentView 的右侧对齐。像这样。

time.textAlignment = NSTextAlignment.Right

我建议您对约束使用以下设置(从 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)