这是我在下面给出的代码:
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(cell.lblname);
NSLog(@"%@",viewsDictionary);
NSArray *constra= [NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[cell.lblname]-10-|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:viewsDictionary];
//Work through the array of constraints, applying each to the superview
for (int i = 0; i<constra.count; i++) {
[cell.contentView addConstraint:constra[i]];
}
这就是我得到的
2015-12-14 16:23:34.846 hello[2354:102290] {
"cell.lblname" = "<UILabel: 0x7fd6a1cb7a80; frame = (0 0; 318 40); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fd6a1cf4610>>";
}
2015-12-14 16:23:34.898 hello[2354:102290] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format:
cell is not a key in the views dictionary.
|-10-[cell.lblname]-10-|
^'
如何解决这个问题对我有帮助,我是视觉约束的新手....
答案 0 :(得分:3)
NSDictionary *viewsDictionary = {@"lblname": self.lblname};
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[lblname]-10-|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:viewsDictionary];
[cell.contentView addConstraints:constraints];