我正在尝试使用视觉格式语言将顶部和底部填充设置为 Autolayout 中的视图。如果我将填充作为可视格式字符串中的整数编写,则代码可以编译并工作,但是当我尝试用常量替换它时它会失败。这是我得到的错误:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法解析约束格式:无法设置等于视图宽度或高度的空间。也许您想使用视图作为分隔符?[view1][spacer(==view1)][view2] V:|-kTopAndBottomPadding-[messageTextView]-kTopAndBottomPadding-|
这是我的代码。
CGFloat const SPMTVC_kTopAndBottomPadding = 5.0;
// ...
// Create my own NSDictionary of variable bindings.
NSDictionary *variableBindings = @{@"messageTextView" : _messageTextView,
@"contentView" : self.contentView,
@"kTopAndBottomPadding" : [NSNumber numberWithFloat:SPMTVC_kTopAndBottomPadding]};
// Constraints in the horizontal axis.
// Basically just pins the view to the left and right of superview.
NSMutableArray *constraints = [[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-0-[messageTextView(==contentView)]-0-|"
options:NSLayoutFormatAlignAllLeading
metrics:nil
views:variableBindings] mutableCopy];
// Constraints in vertical axis, give 5-point padding from superview's top & bottom.
[constraints addObjectsFromArray:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|-kTopAndBottomPadding-[messageTextView]-kTopAndBottomPadding-|"
options:NSLayoutFormatAlignAllTop
metrics:nil
views:variableBindings]];
for (NSLayoutConstraint *constraint in constraints) {
[self.contentView addConstraint:constraint];
}
我认为错误消息意味着编译器认为kTopAndBottomPadding
是 a UIView
,当它是NSNumber
字典中定义的 a 时。有没有办法做到这一点?