gpt4 book ai didi

ios - 为什么这个堆栈 View 不能在 Playground 上工作?

转载 作者:行者123 更新时间:2023-11-28 08:04:45 25 4
gpt4 key购买 nike

有什么想法吗?

    import UIKit
import PlaygroundSupport

extension CGRect {
init(_ x:CGFloat, _ y:CGFloat, _ w:CGFloat, _ h:CGFloat) {
self.init(x:x, y:y, width:w, height:h)
}
}

let v0 = UIView(frame: CGRect(0, 0, 500, 500))
v0.backgroundColor = .white

let v1 = UIView(frame: CGRect(0,0,500,100))
v1.backgroundColor = .red
v1.translatesAutoresizingMaskIntoConstraints = false

let v2 = UIView(frame: CGRect(0,200,500,100))
v2.backgroundColor = .yellow
v2.translatesAutoresizingMaskIntoConstraints = false

let v3 = UIView(frame: CGRect(0,400,500,100))
v3.backgroundColor = .blue
v3.translatesAutoresizingMaskIntoConstraints = false

let sv = UIStackView(frame: CGRect(0, 0, 500, 500))
sv.addArrangedSubview(v1)
sv.addArrangedSubview(v2)
sv.addArrangedSubview(v3)
sv.axis = .vertical
sv.alignment = .center
sv.distribution = .fillEqually
sv.translatesAutoresizingMaskIntoConstraints = false
sv.backgroundColor = .orange

v0.addSubview(sv)

NSLayoutConstraint.activate([

sv.topAnchor.constraint(equalTo: v0.topAnchor),
sv.leadingAnchor.constraint(equalTo: v0.leadingAnchor),
sv.trailingAnchor.constraint(equalTo: v0.trailingAnchor),
sv.bottomAnchor.constraint(equalTo: v0.bottomAnchor),
])

PlaygroundPage.current.liveView = v0

这是我看到的...

enter image description here

最佳答案

在相反的轴上,它需要限制大小。

因此,我在 stackview 处于垂直模式时添加了这些约束......

v1.widthAnchor.constraint(equalTo: v0.widthAnchor)
v2.widthAnchor.constraint(equalTo: v0.widthAnchor)
v3.widthAnchor.constraint(equalTo: v0.widthAnchor)

并且在水平...

v1.heightAnchor.constraint(equalTo: v0.heightAnchor)
v2.heightAnchor.constraint(equalTo: v0.heightAnchor)
v3.heightAnchor.constraint(equalTo: v0.heightAnchor)

也可以删除彩色 View 上的框架。

关于ios - 为什么这个堆栈 View 不能在 Playground 上工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45495430/

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