我想替換UIButton的約束設置框架,但我的代碼不斷崩潰,我做錯了什麼?約束替換UIButton框架
- (void)CreateButton {
self.Button = [[UIButton alloc] init];
//self.Button.frame = CGRectMake(30, 30, 100, 100);
[self.Button addConstraint:[NSLayoutConstraint constraintWithItem:MyScrollView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.myButton
attribute:NSLayoutAttributeTop
multiplier:2.0
constant:30]];
[self.myButton addConstraint:[NSLayoutConstraint constraintWithItem:MyScrollView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.myButton
attribute:NSLayoutAttributeHeight
multiplier:3.0
constant:50]];
[self.myButton setBackgroundColor:[UIColor orangeColor]];
[self.myButton setTitle:@"Press Me" forState:UIControlStateNormal];
[self.myButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[MyScrollView addSubview:self.myButton];
*「Crashing」*,嗯?如果向我們顯示錯誤消息**和**,告訴我們導致崩潰的代碼的**確切**行,我們可能會有更多幫助。 – luk2302
假設你想爲單個按鈕設置約束條件,至少你在上面的代碼(self.Button和self.myButton) – slxl
中有兩個不同的地方,並且爲了簡單起見,試圖使用obj-C命名約定。這將有助於您避免將來出現此類問題https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html – slxl