gpt4 book ai didi

ios - 如何安排 `UIStackView` 中的 View ?

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

我想在 UIStackView 中设置四个水平方向的 View 。四个 View 相邻,没有空格。

enter image description here

我的代码:

let arrangeStackView = UIStackView()
let followUpActionView = UIView()
let developCurriculumView = UIView()
let moreMoreView = UIView()
let takeCareSalonView = UIView()

override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(arrangeStackView)

developCurriculumView.translatesAutoresizingMaskIntoConstraints = false;
followUpActionView.translatesAutoresizingMaskIntoConstraints = false;
takeCareSalonView.translatesAutoresizingMaskIntoConstraints = false;
moreMoreView.translatesAutoresizingMaskIntoConstraints = false;

developCurriculumView.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)
followUpActionView.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)
takeCareSalonView.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)
moreMoreView.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)


arrangeStackView.axis = .horizontal
arrangeStackView.distribution = .fillEqually

arrangeStackView.spacing = 10
arrangeStackView.alignment = .fill

arrangeStackView.translatesAutoresizingMaskIntoConstraints = false
arrangeStackView.addSubview(followUpActionView)
arrangeStackView.addSubview(developCurriculumView)
arrangeStackView.addSubview(moreMoreView)
arrangeStackView.addSubview(takeCareSalonView)


arrangeStackView.snp.makeConstraints { (make) in
make.center.equalToSuperview()
make.height.equalTo(95)
make.width.equalToSuperview()
}

let yaIconWith = self.view.frame.width * 0.25

followUpActionView.snp.makeConstraints{(make) -> Void in
make.height.equalToSuperview()
make.width.equalTo(yaIconWith)
}
takeCareSalonView.snp.makeConstraints{(make) -> Void in
make.height.equalToSuperview()
make.width.equalTo(yaIconWith)
}
moreMoreView.snp.makeConstraints{(make) -> Void in
make.height.equalToSuperview()
make.width.equalTo(yaIconWith)
}
developCurriculumView.snp.makeConstraints{(make) -> Void in
make.height.equalToSuperview()
make.width.equalTo(yaIconWith)
}

现在,我看结果如下。

enter image description here

为什么这四个 View 位于屏幕的左上角?

最佳答案

您将每个 View 作为 subview 添加到堆栈 View 中,而不是将每个 View 作为排列的 subview 添加。虽然它们听起来很相似,但只有排列好的 subview 是根据堆栈 View 的属性进行布局的。

因此您将更改这些行:

arrangeStackView.addSubview(followUpActionView)
arrangeStackView.addSubview(developCurriculumView)
arrangeStackView.addSubview(moreMoreView)
arrangeStackView.addSubview(takeCareSalonView)

对此:

arrangeStackView.addArrangedSubview(followUpActionView)
arrangeStackView.addArrangedSubview(developCurriculumView)
arrangeStackView.addArrangedSubview(moreMoreView)
arrangeStackView.addArrangedSubview(takeCareSalonView)

如果您需要特殊安排,或者如果您需要在某些地方的不同时间添加 View ,insertArrangedSubview(_:at:)addArrangedSubview(_:) 的替代方法

关于ios - 如何安排 `UIStackView` 中的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48172752/

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