gpt4 book ai didi

swift - UIStackView 拉伸(stretch) subview 并对齐它

转载 作者:行者123 更新时间:2023-11-30 10:59:17 26 4
gpt4 key购买 nike

我正在尝试以编程方式布局 StackView,我想要实现的效果是<强> strong text

但是我得到了

enter image description here

我不明白为什么loadingDotView会拉伸(stretch)以填满所有空间?

   let loadingDotView: UIView = {
let ldv = UIView()
ldv.backgroundColor = .white
ldv.alpha = 0
ldv.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
ldv.layer.cornerRadius = 10
ldv.layer.masksToBounds = true
ldv.translatesAutoresizingMaskIntoConstraints = false
return ldv
}()

let dotsStackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.distribution = .equalSpacing
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()

设置代码...

   view.addSubview(dotsStackView)



NSLayoutConstraint.activate([
dotsStackView.heightAnchor.constraint(equalToConstant: 20),
dotsStackView.widthAnchor.constraint(equalToConstant: 100),
dotsStackView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
dotsStackView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])



dotsStackView.addArrangedSubview(loadingDotView)
dotsStackView.addArrangedSubview(loadingDotView)
dotsStackView.addArrangedSubview(loadingDotView)

最佳答案

这个(关闭)

let loadingDotView: UIView = {
let ldv = UIView()
ldv.backgroundColor = .white
ldv.alpha = 0
ldv.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
ldv.layer.cornerRadius = 10
ldv.layer.masksToBounds = true
ldv.translatesAutoresizingMaskIntoConstraints = false
return ldv
}()

每次访问都返回相同的对象,因此只出现一个对象,使其(计算属性)每次访问都创建一个新对象

var loadingDotView: UIView {
let ldv = UIView()
ldv.backgroundColor = .white
ldv.alpha = 0
ldv.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
ldv.layer.cornerRadius = 10
ldv.layer.masksToBounds = true
ldv.translatesAutoresizingMaskIntoConstraints = false
return ldv
}

并添加

stackView.spacing = 20
stackView.distribution = .fillEqually

enter image description here

关于swift - UIStackView 拉伸(stretch) subview 并对齐它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53592848/

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