【问题标题】:NSAutoLayoutConstraints crashing appNSAutoLayoutConstraints 崩溃的应用程序
【发布时间】:2015-02-21 03:22:15
【问题描述】:

我学习了一些教程,但我似乎无法让我的应用接受我为 NSAutoLayoutConstraints 制定的规则。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: 
Unable to interpret '|' character, because the related view doesn't have a superview 
V:|-[signupBtn][signupWithFacebookBtn]-| 

在我的 viewLoad 方法中:

-(void)loadView
{
    [super loadView];



    //Create the background view
    [self.view setBackgroundColor:[UIColor whiteColor]];


    //Set up the buttons, labels, and textfields
    signupBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [signupBtn setTitle:@"Sign Up!" forState:UIControlStateNormal];
    [signupBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
    [signupBtn setFrame:CGRectMake(50, 350, 220, 40)];
    signupWithFacebookBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [signupWithFacebookBtn setTitle:@"Sign Up with Facebook" forState:UIControlStateNormal];
    [signupWithFacebookBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
    [signupWithFacebookBtn setFrame:CGRectMake(50, 400, 320, 40)];



    //A dictionary of all subviews
    NSDictionary* m_viewsDictionary = NSDictionaryOfVariableBindings(signupBtn, signupWithFacebookBtn);

    //Constraints
    NSArray* verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[signupBtn][signupWithFacebookBtn]-|" options:0 metrics:nil views:m_viewsDictionary];
    NSArray* horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[signupBtn][signupWithFacebookBtn]-|" options:0 metrics:nil views:m_viewsDictionary];



    //Add the buttons, labels and textfields
    [self.view addSubview:signupBtn];
    [self.view addSubview:signupWithFacebookBtn];

    //Add constraints
    [self.view addConstraints:verticalConstraints];
    [self.view addConstraints:horizontalConstraints];


    NSLog(@"Hello world");
}

我不确定它为什么会崩溃。子视图存在,并且在添加约束之前被添加。此外,两个按钮的setTranslatesAutoresizeMaskIntoConstraints 都设置为NO。 我错过了什么?

【问题讨论】:

    标签: ios objective-c autolayout


    【解决方案1】:

    您应该在定义约束之前将按钮添加为子视图——解析器不理解“|”是什么是因为您的按钮在创建约束时没有超级视图。

    【讨论】:

    • 啊!我懂了。我误解了,并认为必须在添加子视图后添加约束,但未定义。谢谢!
    猜你喜欢
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多