gpt4 book ai didi

ios - 将 View 限制为其 super View 的最简单方法

转载 作者:行者123 更新时间:2023-11-30 11:02:09 25 4
gpt4 key购买 nike

我有一个名为 containerViewUIView其中包含我 View 中的所有 UI 元素。我制作 containerView 的原因首先是因为我想为屏幕较小的设备实现键盘支持,并且我无法嵌入 self.view进入UISCrollView

当我对我的 containerView 设置约束时,我这样做:

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

/* constraints of containerView's subviews */
}

因为我限制了我的 containerView 的所有四个 anchor 至view ,我很想知道是否有一种不太冗长的方法(也许是一句简单的话?)可以实现这一目标。

最佳答案

使用内置方法,我相信这将是最短的方法。

let views = ["contentView": contentView]
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "|-[contentView]-|", views: views))
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[contentView]-|", views: views))

但是这是您经常做的事情,创建一个可以调用的扩展将是一个更好的选择。

关于ios - 将 View 限制为其 super View 的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53216199/

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