Heim > Fragen und Antworten > Hauptteil
// NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(self.segmentControl);
NSDictionary * viewsDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:self.segmentControl ,@"segmentControl", nil];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[segmentControl]" options:0 metrics:nil views:viewsDictionary]];
// 第一句显示错误,运行不通过,显示不合法的key,原因如下:
// terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: segmentControl is not a key in the views dictionary.
我想知道,为什么第一句不对,而第二句对的。
伊谢尔伦2017-04-22 09:02:26
NSDictionary * viewsDictionary = @{@"segmentControl":self.segmentControl};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[segmentControl]" options:0 metrics:nil views:viewsDictionary]];
因为views中的字典是解释-[segmentControl]是哪个视图
你的命名有严重的错误,[self.view addConstraints View上面添加控制器进行约束???