我想替换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