替换UIButton框架的约束

时间:2016-05-26 18:16:48

标签: objective-c uibutton constraints frame nslayoutconstraint

我正在尝试用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];

1 个答案:

答案 0 :(得分:0)

您应该将约束添加到MyScrollView。