我正在尝试以编程方式使用自动版式并遇到一些问题。我的代码很简单。我有一个视图,我正在尝试使用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吗?

关于ios - NSLayoutConstraint引发异常“不是 View 字典中的键”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27757249/

10-10 15:21