向底部锚添加约束是向超级视图底部添加约束
Posted
技术标签:
【中文标题】向底部锚添加约束是向超级视图底部添加约束【英文标题】:Adding constraint to bottom anchor is adding to constraint to superview bottom 【发布时间】:2019-08-25 16:47:45 【问题描述】:我正在尝试将 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];
但是,实际结果是:
这可能是什么原因?
【问题讨论】:
【参考方案1】:你可以使用
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)
])
【讨论】:
以上是关于向底部锚添加约束是向超级视图底部添加约束的主要内容,如果未能解决你的问题,请参考以下文章