2つのビューがあります。 subLabel
とtinyImage
iOSで横方向の拘束がクラッシュする
私は、tinyImageを10ピクセルの間隔でsubLabelの右にしたいと思います。私はこのコードを実行すると
let horizontalConstraint = NSLayoutConstraint(item: subLabel, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: tinyImage, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 10)
tinyImage.addConstraint(horizontalConstraint)
は、それがクラッシュし、私のようなものを参照してください:これは私が持っているものである
The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fe89a6ece10 UILabel:0x7fe89a764cf0'2'.trailing == UIImageView:0x7fe89a765bb0.leading + 10>
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(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
tinyImageとsubLabelの兄弟ビューはありますか? –
'tinyImage'と' subLabel'の両方がビュー階層に追加されていることを確認し、 'tinyImage'ではなくcommon superviewに制約を加えるべきです。おそらく' self.addConstraint'か 'self.view。 'self'が何であるかに応じてaddConstraint'を返します – Paulw11
@ReyGonzalesはい、infoViewは両方のビューをサブビューとして追加します。 – TIMEX