Swift - UITableViewCell类contentView以编程方式约束并不具有宽度

时间:2015-01-13 18:07:36

标签: ios uitableview swift constraints

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的旁边: portrait mode 即使在横向模式下,contentView也无法覆盖所有屏幕: landscape mode

我已尝试使用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宽度那么长。我不知道......

3 个答案:

答案 0 :(得分:1)

尝试在superview上添加contentView约束:

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

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

self.setTranslatesAutoresizingMaskIntoConstraints(false)

答案 1 :(得分:1)

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

time.textAlignment = NSTextAlignment.Right

答案 2 :(得分:0)

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