gpt4 book ai didi

ios - Swift 如何根据内容设置 UIView 的高度限制

转载 作者:可可西里 更新时间:2023-11-01 02:02:30 25 4
gpt4 key购买 nike

我的 swift 代码中有一个 UIView

let profile_inf_wrapper: UIView = {
let v = UIView()
v.backgroundColor = .red
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()

profile_inf_wrapper.topAnchor.constraint(equalTo: view.topAnchor, constant:64).isActive = true
profile_inf_wrapper.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
profile_inf_wrapper.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
profile_inf_wrapper.heightAnchor.constraint(equalToConstant: view.frame.height/4).isActive = true

backgroundImageView.topAnchor.constraint(equalTo: profile_inf_wrapper.topAnchor).isActive = true
backgroundImageView.leftAnchor.constraint(equalTo: profile_inf_wrapper.leftAnchor).isActive = true
backgroundImageView.rightAnchor.constraint(equalTo: profile_inf_wrapper.rightAnchor).isActive = true
backgroundImageView.bottomAnchor.constraint(equalTo: profile_inf_wrapper.bottomAnchor).isActive = true

profileImage.centerYAnchor.constraint(equalTo: profile_inf_wrapper.centerYAnchor).isActive = true
profileImage.leftAnchor.constraint(equalTo: view.leftAnchor, constant:25).isActive = true
profileImage.widthAnchor.constraint(equalToConstant: 110).isActive = true
profileImage.heightAnchor.constraint(equalToConstant: 110).isActive = true

usernameLabel.topAnchor.constraint(equalTo: profile_inf_wrapper.topAnchor, constant:40).isActive = true
usernameLabel.leftAnchor.constraint(equalTo: profileImage.rightAnchor, constant:20).isActive = true

countryIcon.topAnchor.constraint(equalTo: usernameLabel.bottomAnchor, constant:10).isActive = true
countryIcon.leftAnchor.constraint(equalTo: profileImage.rightAnchor, constant:20).isActive = true
countryIcon.widthAnchor.constraint(equalToConstant: 25).isActive = true
countryIcon.heightAnchor.constraint(equalToConstant: 25 ).isActive = true

countryName.topAnchor.constraint(equalTo: usernameLabel.bottomAnchor, constant:5).isActive = true
countryName.leftAnchor.constraint(equalTo: countryIcon.rightAnchor, constant:10).isActive = true
countryName.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

所有这些元素都是 profile_inf_wrapper 的 subview 。有时 view.frame.height/4 太小,我希望能够根据它调整 UIView 的大小内容

最佳答案

UIView 中有一个属性称为 intrinsicContentSize .它返回 View 显示其所有内容所需的最小尺寸。

虽然默认实现不是很有用,因为 UIView它自己没有任何内容,所有默认子类都实现了它。

A UILabel将返回一个完全适合文本的大小,以及一个 UIButton将返回适合其内容的大小加上您添加的任何间距。你明白了它的要点。

您可以通过仅约束 width 来利用此属性或 height的观点,而不是两者。如果你约束 widthUILabel并添加更多文本,它会垂直增长。

最后,当您向 UIView 添加 subview 时,并且您向轴的两个边距添加约束( top and bottomleft and right ),只要存在约束和 View 的“链”,并且 View 对大小没有任何约束,它就会扩展适合。

比如你有一个view,有一个label和一个button,垂直排列,如果label约束在上面,那么约束在button上,button约束在下面,只要容器 View 没有 height约束,它将扩展以完美地适应两个 View 和边距。

您的目标应该始终是使用最少的约束来表达您的设计,而不删除有用的约束。确保利用 intrinsicContentSize .

关于ios - Swift 如何根据内容设置 UIView 的高度限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45387363/

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