ios - NSLayoutConstraint 抛出异常 "is not a key in the views dictionary"

标签 ios autolayout

我正在尝试以编程方式使用自动布局并面临一些问题。我的代码很简单。我有一个 View ,我正在尝试使用 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 抛出异常 "is not a key in the views dictionary",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27757249/

相关文章:

ios - xcode/tesseract,使用图片库中的图片

ios - 通过嵌入式 View Controller 返回导航 Controller 内部

swift - 无法同时满足约束 - 制图

ios - 使用自动布局实现按钮的网格布局

ios - 如何在 JSQMessageView Controller 中添加静态 header

ios - 设置包根中没有 'Child Pane' 选项

ios - 关于单元重用的自定义 UITableViewCell 约束问题

ios - UIButton 在 UIScrollView 中不工作/不可点击

ios - NSLAyoutConstraints 不适用于 UICollectionViewCell 中的 UIView

ios - 没有明确的宽度约束和更大或相等的尾随空间行为不当的自动布局