我正在尝试向UIImageview添加约束,以使其像这样在安全区域布局指南中位于前,后,顶部和底部:
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];
}
但是,实际结果是:
这可能是什么原因?
答案 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)
])