gpt4 book ai didi

ios - 设置间距时 UIStackView 的约束会中断

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

当我尝试使用 UIStackView 时,XCode 抛出以下约束错误:

(
"<NSAutoresizingMaskLayoutConstraint:0x7f87a1dfa360 h=--& v=--& V:[UIStackView:0x7f87a6403a00(0)]>",
"<NSLayoutConstraint:0x7f87a6410590 'UISV-canvas-connection' UIStackView:0x7f87a6403a00.top == UIView:0x7f87a6409630.top>",
"<NSLayoutConstraint:0x7f87a6444170 'UISV-canvas-connection' V:[UIView:0x7f87a644d790]-(0)-| (Names: '|':UIStackView:0x7f87a6403a00 )>",
"<NSLayoutConstraint:0x7f87a645bec0 'UISV-fill-equally' UIView:0x7f87a6407a10.height == UIView:0x7f87a6409630.height>",
"<NSLayoutConstraint:0x7f87a6458f40 'UISV-fill-equally' UIView:0x7f87a644d790.height == UIView:0x7f87a6409630.height>",
"<NSLayoutConstraint:0x7f87a64306d0 'UISV-spacing' V:[UIView:0x7f87a6409630]-(5)-[UIView:0x7f87a6407a10]>",
"<NSLayoutConstraint:0x7f87a643bea0 'UISV-spacing' V:[UIView:0x7f87a6407a10]-(5)-[UIView:0x7f87a644d790]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7f87a643bea0 'UISV-spacing' V:[UIView:0x7f87a6407a10]-(5)-[UIView:0x7f87a644d790]>

我的 View Controller 如下:

public class ExampleController: UIViewController {

let v1 = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 100))

let v2 = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 300))

let v3 = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 200))

let parent1 = UIStackView()


public override func viewDidLoad() {
super.viewDidLoad()

v1.backgroundColor = .red
v2.backgroundColor = .green
v3.backgroundColor = .blue

parent1.axis = .vertical
parent1.distribution = .fillEqually
parent1.spacing = 5 // TODO: This causes the error!

parent1.addArrangedSubview(v1)
parent1.addArrangedSubview(v2)
parent1.addArrangedSubview(v3)

view.addSubview(parent1)
}

// MARK: Constraints

private var didUpdateConstraints = false

override public func updateViewConstraints() {
if !didUpdateConstraints {

parent1.snp.makeConstraints { (make) -> Void in
make.edges.equalToSuperview()
}

didUpdateConstraints = true
}
super.updateViewConstraints()
}
}

堆栈 View 的分布似乎没有什么不同。每当设置间距时,我都会收到错误消息。


  1. 与我的约束有什么冲突?

  2. 什么是UISV-canvas-connection

最佳答案

它实际上并没有因为间距而断裂。它因为您创建 View 的方式而中断 :D

当你创建一个像...这样的 View 时

let v1 = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 100))

当 View 被布局时,它会添加一组默认的约束。 UIStackView 也是如此。

当你创建它时...

let parent1 = UIStackView()

它获得一个 (0, 0, 0, 0) 的框架,然后向其添加约束以保持该框架的位置和高度。

你得到的错误是因为堆栈 View 的高度和宽度。

<NSAutoresizingMaskLayoutConstraint:0x7f87a1dfa360 h=--& v=--& V:[UIStackView:0x7f87a6403a00(0)]>

这一行指的是那些“自动”约束。

您最好的选择是使用约束重新创建 View 的宽度和高度。

像这样...

// using this format means you can create the view and add all the
// other config to it at the same time
let v1: UIView = {
let u = UIView()

// this line stops the "automatic" constraints being added
u.translatesAutoresizingMaskIntoConstraints = false

u.backgroundColor = .red

// now you have to add your own constraints though
u.heightAnchor.constraint(equalToConstant: 100).isActive = true
u.widthAnchor.constraint(equalToConstant: 250).isActive = true
return u
}()

let v2: UIView = {
let u = UIView()
u.translatesAutoresizingMaskIntoConstraints = false
u.backgroundColor = .green
u.heightAnchor.constraint(equalToConstant: 100).isActive = true
u.widthAnchor.constraint(equalToConstant: 250).isActive = true
return u
}()

let v3: UIView = {
let u = UIView()
u.translatesAutoresizingMaskIntoConstraints = false
u.backgroundColor = .blue
u.heightAnchor.constraint(equalToConstant: 100).isActive = true
u.widthAnchor.constraint(equalToConstant: 250).isActive = true
return u
}()

let parent1: UIStackView = {
let s = UIStackView()
s.translatesAutoresizingMaskIntoConstraints = false
s.spacing = 5
s.axis = .vertical
s.distirbution = .fillEqually
return s
}()

public override func viewDidLoad() {
super.viewDidLoad()

// I moved all the config stuff into the creation of each view...
// No need to have them here.

parent1.addArrangedSubview(v1)
parent1.addArrangedSubview(v2)
parent1.addArrangedSubview(v3)

view.addSubview(parent1)
}

这里有个警告。

您正在堆栈 View 中使用 .fillEqually。这将使每个 View 的高度彼此相等。这将与您添加到它们的高度约束冲突。也许您应该完全移除每个 View 的约束,让堆栈 View 进行布局。

像这样...

let v2: UIView = {
let u = UIView()
u.translatesAutoresizingMaskIntoConstraints = false
u.backgroundColor = .green
return u
}()

关于ios - 设置间距时 UIStackView 的约束会中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41762349/

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