【发布时间】:2018-09-11 07:11:48
【问题描述】:
我想在没有任何参考的情况下使用情节提要获得所有应用约束的参考:
我尝试了很多方法,但无法找到确切的解决方案:
我的方法如下:
if let constraint = (self.constraints.filter{$0.firstAttribute == .height}.first) {
}
使用上述方法,我只能找出高度。
if let topConstraint = (self.constraints.filter{$0.firstAttribute == .top}.first) {
topConstraint.constant = 150//topMargin
}
if let leadingConstraint = (self.constraints.filter{$0.firstAttribute == .leading}.first) {
leadingConstraint.constant = 60 //leadingMargin
}
对于 topConstraint 和leadingConstraint 我得到了零。
self.constraints
self.constraints 只给出一个参考,即只有高度,即使我在同一个视图上应用了前导、尾随和底部约束。
注意:我不想从情节提要中获取参考,因此请不要建议该解决方案。我想要动态引用。
我正在寻找类似以下的方法:
if let topConstraint = (self.constraints.filter{$0.firstAttribute == .top}.first) {
topConstraint.constant = 150//topMargin
}
if let leadingConstraint = (self.constraints.filter{$0.firstAttribute == .leading}.first) {
leadingConstraint.constant = 60 //leadingMargin
}
if let trailingConstraint = (self.constraints.filter{$0.firstAttribute == .trailing}.first) {
trailingConstraint.constant = 70//leadingMargin
}
if let bottomConstraint = (self.constraints.filter{$0.firstAttribute == .bottom}.first) {
bottomConstraint.constant = 150//49 + bottomMargin
}
但不幸的是,上面的一个对我不起作用:(
【问题讨论】:
-
@bhatejaud我已经使用情节提要应用了我的约束,并希望在运行时访问它的引用。这两个问题是不同的。如果您有任何解决方案,请建议我。
-
@Alok 我认为您可以通过标识符获得约束。这是链接苹果文档developer.apple.com/documentation/uikit/nslayoutconstraint/…
-
嗯...实际上,您的问题让我说:您这样做的目的是什么?!即一般问题是什么?
-
@chiragshah 感谢您的建议,但我不想采用这种方法,因为我有 50 多个控制器并且想在任何地方进行更改。所以这种方法会导致运行时错误。你有没有像问题中给出的身高一样的方法?如果让约束 = (self.constraints.filter{$0.firstAttribute == .height}.first) { }
标签: ios swift autolayout ios-autolayout