gpt4 book ai didi

swift - 将 layoutMargins 添加到 UIStackView 中的一个元素

转载 作者:行者123 更新时间:2023-12-02 00:54:30 24 4
gpt4 key购买 nike

我想创建一个包含 3 个元素的垂直堆栈 View 。
我只想要第二个和最后一个元素之间有更多的空间。所以我想添加到最后一个元素:

mylastelement.layoutMargins = UIEdgeInsets(top:30, left:0,bottom:0, right:0)

但是我的堆栈 View 中没有应用 layoutmargins。有没有什么简单的方法可以实现(我想避免修改最后一个元素的内部高度)。

编辑:我只是尝试通过执行以下操作来增加其框架内的第二个元素高度(+50):
my2ndElementLabel.sizeToFit()
my2ndElementLabel.frame = CGRect(x:my2ndElementLabel.frame.origin.x,y:lmy2ndElementLabel.frame.origin.y,
width:my2ndElementLabel.frame.width, height:my2ndElementLabel.frame.height + 50)

但它没有效果。

EDIT2:我试图向我的 UIStackView 添加一个随机 View ,但该 View 被忽略了!可能在理解 UIKit 的工作方式时遗漏了一些东西?...:
let v = UIView(frame:CGRect(x:0,y:0,width:100,height:400))
v.backgroundColor = .red
myStackView.addArrangedSubview(v)
//...

最佳答案

这是我做的一个扩展,有助于快速实现这样的利润:

extension UIStackView {

func addArrangedSubview(_ v:UIView, withMargin m:UIEdgeInsets )
{
let containerForMargin = UIView()
containerForMargin.addSubview(v)
v.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
v.topAnchor.constraint(equalTo: containerForMargin.topAnchor, constant:m.top ),
v.bottomAnchor.constraint(equalTo: containerForMargin.bottomAnchor, constant: m.bottom ),
v.leftAnchor.constraint(equalTo: containerForMargin.leftAnchor, constant: m.left),
v.rightAnchor.constraint(equalTo: containerForMargin.rightAnchor, constant: m.right)
])

addArrangedSubview(containerForMargin)
}
}

关于swift - 将 layoutMargins 添加到 UIStackView 中的一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55441284/

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