我在运行时创建了一个UIButton。当我点击按钮时,我的应用程序将崩溃并显示错误无法在视图上安装约束。。
-(void)loadView{
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
scrollView.contentSize=CGSizeMake(320,758);
scrollView.contentInset=UIEdgeInsetsMake(20.0,0.0,44.0,0.0);
scrollView.scrollIndicatorInsets=UIEdgeInsetsMake(20.0,0.0,44.0,0.0);
UIImage *red = [UIImage imageNamed:@"red.png"];
UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(165, y, 142, 35)];
cancelButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
[cancelButton setBackgroundImage:red forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:cancelButton];
}
- (IBAction)cancelButtonPressed:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil]; --> error
}
错误
由于未捕获的异常'NSGenericException'而终止应用程序,原因:'无法在视图上安装约束。约束是否引用了视图子树之外的内容?那是违法的。约束:view:; layer =; contentOffset:{0,0}>'