视图不遵守约束

时间:2020-04-29 17:48:45

标签: ios objective-c autolayout constraints

首先,我想说我在使用代码创建视图方面相对较新。 我在这里想要做的是用另一个(productView)替换一个视图(_addProductView),并在(productView)和(_initialDateView)之间添加一个(addButton)。问题在于这些视图没有遵守我给它们的约束。

    NewProductView *productView = [NewProductView loadFromNib];
    [_addProductView removeFromSuperview];

    AddProductView *addButton = [AddProductView loadFromNib];

    productView.translatesAutoresizingMaskIntoConstraints = false;
    _initialDateView.translatesAutoresizingMaskIntoConstraints = false;
    addButton.translatesAutoresizingMaskIntoConstraints = false;

    [self.view addSubview:addButton];
    [self.view addSubview:productView];

    [productView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:0].active = YES;
    [productView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:0].active = YES;
    [productView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:8.0].active = YES;
    [productView.bottomAnchor constraintEqualToAnchor:addButton.topAnchor constant:8.0].active = YES;

    [addButton.bottomAnchor constraintEqualToAnchor:_initialDateView.topAnchor constant:8.0].active = YES;
    [addButton.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:0].active = YES;
    [addButton.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:0].active = YES;

enter image description here

首先,日期视图不应太大。它应该像最终的日期视图。以蓝色突出显示的视图是我的(addButton),应该具有各自顶部的8个顶部和8个底部。我在这里想念什么?

0 个答案:

没有答案