這是鏈接到視圖控制器的非常基本的視圖。該視圖具有一個單獨的UILabel,它與編碼約束一起放置。代碼實際上運行正常,但控制檯正在註冊一個約束衝突,並且我找不出哪部分代碼正在創建衝突。iOS以編程方式生成的視圖具有隱藏約束,導致與自動佈局的衝突
LocationView.m文件
#import "LocationView.h"
@implementation LocationView
@synthesize locationTitle;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setBackgroundColor: [UIColor blueColor]];
locationTitle = [[UILabel alloc]init];
[locationTitle setTranslatesAutoresizingMaskIntoConstraints:NO];
locationTitle.backgroundColor = [UIColor whiteColor];
[self addSubview:locationTitle];
NSDictionary *viewLocationTitle = NSDictionaryOfVariableBindings(locationTitle);
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[locationTitle]|"
options:0
metrics:0
views:viewLocationTitle]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[locationTitle(50)]|"
options:0
metrics:0
views:viewLocationTitle]];
- (void)updateConstraints {
[super updateConstraints];
}
@end
控制檯錯誤
Unable to simultaneously satisfy constraints.
(
"<NSLayoutConstraint:0x109510990 V:|-(NSSpace(20))-[UILabel:0x10950b660] (Names: '|':LocationView:0x10950acc0)>",
"<NSLayoutConstraint:0x109510c10 V:[UILabel:0x10950b660(50)]>",
"<NSLayoutConstraint:0x109510c60 V:[UILabel:0x10950b660]-(0)-| (Names: '|':LocationView:0x10950acc0)>",
"<NSAutoresizingMaskLayoutConstraint:0x108f3ae00 h=--& v=--& V:[LocationView:0x10950acc0(568)]>"
)