繁体   English   中英

来自xib的ios8加载视图:无法同时满足约束

[英]ios8 loading view from xib: Unable to simultaneously satisfy constraints

我有一个非常简单的设置:

我在xib有带有UI的空VC和UIView

MyView.m:

- (void) commonInit {
    [[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0];
    [self addSubview:self.view];
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];

    if (self) {
        [self commonInit];
    }

    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) {
        [self commonInit];
        self.bounds = self.view.bounds;
    }

    return self;
}

还有我的MyVC.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myView = [[MyView alloc] init];
    NSDictionary *bindings = @{@"myView": self.myView};
    [self.view addSubview:self.offlinePromoView];

    self.myView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.myView
                                                          attribute:NSLayoutAttributeCenterX
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(topSpacing)-[myView]-|" options:0 metrics:@{@"topSpacing": @70} views:bindings]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[myView]-|" options:0 metrics:nil views:bindings]];
}

我希望子视图从顶部填充70点,并且宽度与控制器视图相同。 但是我得到:

(
    "<NSLayoutConstraint:0x176ae8d0 MyView:0x176a7770.centerX == UIView:0x176a7840.centerX>",
    "<NSLayoutConstraint:0x176b10b0 MyView:0x176a7770.leading == UIView:0x176a7840.leadingMargin>",
    "<NSLayoutConstraint:0x176b0900 UIView:0x176a7840.trailingMargin == MyView:0x176a7770.trailing>"
)

0x176a7840self.view在我的VC。

我究竟做错了什么?

尝试这样:-

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[myView]-(-70)-|" options:0 metrics:nil views:bindings]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[myView]|" options:0 metrics:nil views:bindings]];
NSDictionary *bindings = NSDictionaryOfVariableBindings(view);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(==70)-[view]-|" options:0 metrics:nil views:bindings]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view]-|" options:0 metrics:nil views:bindings]];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM