gpt4 book ai didi

ios - 将可视格式的约束转换为 NSLayoutAnchor API

转载 作者:搜寻专家 更新时间:2023-11-01 07:08:06 25 4
gpt4 key购买 nike

我从 this SO question a few months ago 获得了以下扩展名,我想将其转换为 NSLayoutAnchor(或更详细的布局 API),原因很简单,即视觉格式不支持指定安全区域布局指南。

链接中的扩展是这个:

extension UIView {

/// Adds constraints to this `UIView` instances `superview` object to make sure this always has the same size as the superview.
/// Please note that this has no effect if its `superview` is `nil` – add this `UIView` instance as a subview before calling this.
func bindFrameToSuperviewBounds() {
guard let superview = self.superview else {
print("Error! `superview` was nil – call `addSubview(view: UIView)` before calling `bindFrameToSuperviewBounds()` to fix this.")
return
}

self.translatesAutoresizingMaskIntoConstraints = false
superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[subview]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self]))
superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[subview]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self]))
}
}

我的 View 层次结构是:

UINavigationController -> Root View Controller :UIPageViewController -> 第一页:UIViewController -> UIScrollView -> UIImageView

当我调用 bindFrameToSuperviewBounds() 时,结果如下:

enter image description here

这看起来大部分都很好。问题是 ImageView 位于导航栏下方,这是我不想要的。所以为了解决这个问题,我尝试重写 bindFrameToSuperviewBounds() 如下:

guard let superview = self.superview else {
fatalError("Attempting to bound to a non-existing superview.")
}

translatesAutoresizingMaskIntoConstraints = false
self.topAnchor.constraint(equalTo: superview.safeAreaLayoutGuide.topAnchor).isActive = true
self.leadingAnchor.constraint(equalTo: superview.safeAreaLayoutGuide.leadingAnchor).isActive = true
self.bottomAnchor.constraint(equalTo: superview.safeAreaLayoutGuide.bottomAnchor).isActive = true
self.trailingAnchor.constraint(equalTo: superview.safeAreaLayoutGuide.trailingAnchor).isActive = true

结果是这样的:

enter image description here

这次它正确地没有进入导航栏下方,但是......你可以看到它被拉长了,这是我不想要的。

将视觉布局 API 转换为可以考虑安全区域布局指南的东西的正确方法是什么?

为了完成,设置 ScrollView 和 ImageView 的代码如下:

override func viewDidLoad() {
super.viewDidLoad()
imageScrollView.addSubview(imageView)
imageScrollView.layoutIfNeeded()
imageView.bindFrameToSuperviewBounds()
view.addSubview(imageScrollView)
imageView.layoutIfNeeded()
imageScrollView.backgroundColor = .white
imageScrollView.bindFrameToSuperviewBounds()
imageScrollView.delegate = self
}

最佳答案

通过 Fattie 提供的代码,我得到了这个解决方案:

guard let superview = self.superview else {
fatalError("Attempting to bound to a non-existing superview.")
}

translatesAutoresizingMaskIntoConstraints = false
self.topAnchor.constraint(equalTo: superview.safeAreaLayoutGuide.topAnchor).isActive = true
self.leadingAnchor.constraint(equalTo: superview.leadingAnchor).isActive = true
self.bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
self.trailingAnchor.constraint(equalTo: superview.trailingAnchor).isActive = true

(现在我只是将顶部锚定到安全布局是指南。这是否会给 iPhone X 带来问题还有待观察)。

这给出了我期望的结果:

enter image description here

关于ios - 将可视格式的约束转换为 NSLayoutAnchor API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46868766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com