Swift: 如何声明视图后代
Swift: How to declare view descendants
我正在尝试添加以下约束以在 table 单元格内对齐图像。 self 指的是图像视图所在的 UITableViewCell 对象:
var imageViewTest = UIImageView();
self.rowImageView = imageViewTest;
var xAlignmentConstraint = NSLayoutConstraint(
item: imageViewTest,
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: self,
attribute: NSLayoutAttribute.CenterX,
multiplier: 1,
constant: 137
);
NSLayoutConstraint.activateConstraints([xAlignmentConstraint]);
self.addConstraint(xAlignmentConstraint);
当运行代码时,我得到以下错误:
Unable to activate constraint with items because they have no common
ancestor. Does the constraint reference items in different view
hierarchies? That's illegal.
如何将 imageViewTest 声明为 self 的后代?
您需要添加 rowImageView
作为 self 的子视图。
var imageViewTest = UIImageView()
self.rowImageView = imageViewTest
self.addSubview(self.rowImageView)
我正在尝试添加以下约束以在 table 单元格内对齐图像。 self 指的是图像视图所在的 UITableViewCell 对象:
var imageViewTest = UIImageView();
self.rowImageView = imageViewTest;
var xAlignmentConstraint = NSLayoutConstraint(
item: imageViewTest,
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: self,
attribute: NSLayoutAttribute.CenterX,
multiplier: 1,
constant: 137
);
NSLayoutConstraint.activateConstraints([xAlignmentConstraint]);
self.addConstraint(xAlignmentConstraint);
当运行代码时,我得到以下错误:
Unable to activate constraint with items because they have no common ancestor. Does the constraint reference items in different view hierarchies? That's illegal.
如何将 imageViewTest 声明为 self 的后代?
您需要添加 rowImageView
作为 self 的子视图。
var imageViewTest = UIImageView()
self.rowImageView = imageViewTest
self.addSubview(self.rowImageView)