将约束添加到底部锚点将添加到约束到超级视图底部

时间:2019-08-25 16:47:45

标签: ios autolayout constraints

我正在尝试向UIImageview添加约束,以使其像这样在安全区域布局指南中位于前,后,顶部和底部:

safe area constraints

    if (@available(iOS 11.0, *)) {
        [[self.view.safeAreaLayoutGuide.topAnchor constraintEqualToAnchor:self.bgImageView.safeAreaLayoutGuide.topAnchor] setActive:true];
        [[self.view.safeAreaLayoutGuide.bottomAnchor constraintEqualToAnchor:self.bgImageView.safeAreaLayoutGuide.bottomAnchor] setActive:true];
        [[self.view.safeAreaLayoutGuide.leadingAnchor constraintEqualToAnchor:self.bgImageView.safeAreaLayoutGuide.leadingAnchor] setActive:true];
        [[self.view.safeAreaLayoutGuide.trailingAnchor constraintEqualToAnchor:self.bgImageView.safeAreaLayoutGuide.trailingAnchor] setActive:true];
    }  

但是,实际结果是:

enter image description here

这可能是什么原因?

1 个答案:

答案 0 :(得分:0)

您可以使用

NSLayoutConstraint.activate([
        bgImageView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
        bgImageView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
        bgImageView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor),
        bgImageView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor)
        ])