NSLayoutConstraint 抛出异常 "is not a key in the views dictionary"
NSLayoutConstraint throwing exception "is not a key in the views dictionary"
我正在尝试以编程方式使用自动布局,但遇到了一些问题。我的代码很简单。我有一个视图,我正在尝试使用 NSLayoutConstraint 添加标签,如下所示
UILabel* downloadLabel = [[UILabel alloc] init];
[self.leftView addSubview:downloadLabel];
downloadLabel.translatesAutoresizingMaskIntoConstraints = NO;
[downloadLabel setBackgroundColor:[UIColor redColor]];
NSDictionary *downloadlabelDict = NSDictionaryOfVariableBindings(downloadLabel);
NSArray *downloadLabelConstraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[dowloadLabel(40)]"
options:0
metrics:nil
views:downloadlabelDict];
NSArray *downloadLabelConstraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[dowloadLabel(200)]"
options:0
metrics:nil
views:downloadlabelDict];
[downloadLabel addConstraints:downloadLabelConstraint_H];
[downloadLabel addConstraints:downloadLabelConstraint_V];
当我运行应用程序时,它在尝试创建第一个 NSLayoutConstraint 时抛出异常。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format:
dowloadLabel is not a key in the views dictionary.V:[dowloadLabel(40)]
也许您在构建约束的两行中指的是 downloadLabel
而不是 dowloadLabel
?
我正在尝试以编程方式使用自动布局,但遇到了一些问题。我的代码很简单。我有一个视图,我正在尝试使用 NSLayoutConstraint 添加标签,如下所示
UILabel* downloadLabel = [[UILabel alloc] init];
[self.leftView addSubview:downloadLabel];
downloadLabel.translatesAutoresizingMaskIntoConstraints = NO;
[downloadLabel setBackgroundColor:[UIColor redColor]];
NSDictionary *downloadlabelDict = NSDictionaryOfVariableBindings(downloadLabel);
NSArray *downloadLabelConstraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[dowloadLabel(40)]"
options:0
metrics:nil
views:downloadlabelDict];
NSArray *downloadLabelConstraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[dowloadLabel(200)]"
options:0
metrics:nil
views:downloadlabelDict];
[downloadLabel addConstraints:downloadLabelConstraint_H];
[downloadLabel addConstraints:downloadLabelConstraint_V];
当我运行应用程序时,它在尝试创建第一个 NSLayoutConstraint 时抛出异常。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format:
dowloadLabel is not a key in the views dictionary.V:[dowloadLabel(40)]
也许您在构建约束的两行中指的是 downloadLabel
而不是 dowloadLabel
?