我有两个不同约束的按钮写入故事板。 在我的代码,我要告诉我的第一个按钮拍摄第二个按钮(高度,宽度,和位置)相同的约束,但我不知道如何做到这一点,而不必通过“子视图”过客。我有两个插座供我的按钮使用。如何通过编程方式修改UIButton的约束条件
@IBOutlet weak var likesButton: UIButton!
@IBOutlet weak var shareButton: UIButton!
我的界面是建立这样的:
-
VIEW
a)...View
b)...View
c) ContentView
1)likeButton
2)shareButton
3)...
d)...
我想是这样的:
self.contentView.translatesAutoresizingMaskIntoConstraints = false
self.shareButton.removeConstraints(self.shareButton.constraints) // Old Constraints
self.shareButton.addConstraints(self.likeButton.constraints) // New Constraints
self.shareButton.updateConstraintsIfNeeded()
self.shareButton.layoutIfNeeded()
后,我有这样的错误:
*** 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:0x14cfe7600 H:[UIButton:0x14cfb7bd0(40)]> view:<UIButton: 0x14e30fa60; frame = (494 528; 26 31); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x14e30f410>>'
Thx提前。
约束被添加到超级视图,该超级视图持有您试图添加约束的视图,而不是视图本身。 – Rikh
好吧,但是我说要添加约束到shareButton而不是视图? – CizooDev