我有兩個不同約束的按鈕寫入故事板。 在我的代碼,我要告訴我的第一個按鈕拍攝第二個按鈕(高度,寬度,和位置)相同的約束,但我不知道如何做到這一點,而不必通過「子視圖」過客。我有兩個插座供我的按鈕使用。如何通過編程方式修改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