2014-09-30 14 views

回答

0

聲明一個出口:

@property(nonatomic,weak) IBOutlet NSLayoutConstraint *buttonWidthConstraint; 

轉到NIB。選擇你的按鈕並固定它的寬度(Editor - Pin - Width)。這會將寬度約束添加到按鈕約束中。將新創建的約束連接到上面的插座。

而在按鈕單擊事件:

- (IBAction)buttonDidClick:(id)sender 
{ 
    [self.buttonWidthConstraint setConstant:300]; // set new width 
} 

UPD:這背後的想法是,自動排版引擎覆蓋每一個需要它的時候,這樣的手動更改,如視圖幀[查看SETFRAME:]不適用。因此,更改佈局的一些屬性的唯一方法是在佈局級別上工作。佈局約束僅僅是一個公式,如:

item1.attr1 {== | <= | >=} multiplier * {item2.attr2 | none} + constant 

在你的情況,這種約束是爲MyButton.width = 1 * (none) + 70,其中70是Xcode的建議原始寬度一樣簡單。