【发布时间】: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