gpt4 book ai didi

ios - UIScrollView 内的 UIStackView,增长到最大高度然后滚动

转载 作者:搜寻专家 更新时间:2023-10-31 22:17:33 25 4
gpt4 key购买 nike

我希望构建一个 View ,该 View 会随着向其添加 subview 而增长。起初高度将为零,然后随着我添加 subview 而增长,直到达到最大尺寸,然后我希望它变得可滚动。

如果我使用 UIScrollView 中的 UIView 构建它并手动设置从上到下的约束,它会按预期工作。但是,我觉得这应该可以使用 UIStackView,但是当我尝试 ScrollView 不会增长,或者 contentSize 的 scrollView 卡在最大高度时,不会滚动。我已经玩了一段时间,认为它实际上可能是 UIStackView 和 Autolayout 的一个错误,但希望我只是错过了一些东西。将 UIStackView 包装在容器 UIView 中没有帮助。

这是一个完整的 View Controller ,它显示了问题(点击任何地方都会向 ScrollView 添加标签)

import UIKit

class DumbScrollStack: UIViewController {

let stack = UIStackView()
let scrollView = UIScrollView()

override func viewDidLoad() {
super.viewDidLoad()

//Setup
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(addLabel)))
stack.axis = .vertical
view.backgroundColor = .blue
scrollView.backgroundColor = .red

//Add scroll view and setup position
view.addSubview(scrollView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true

//Set maximum height
scrollView.heightAnchor.constraint(lessThanOrEqualToConstant: 100).isActive = true

//add stack
scrollView.addSubview(stack)
stack.translatesAutoresizingMaskIntoConstraints = false

//setup scrollView content size constraints
stack.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
stack.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
stack.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
stack.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
stack.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true

// Keep short if stack height is lower than scroll height
// With this constraint the scrollView grows as expected, but doesn't scroll when it goes over 100px
// Without this constraint the scrollView is always 100px tall but it does scroll
let constraint = scrollView.heightAnchor.constraint(equalTo: stack.heightAnchor)
constraint.priority = UILayoutPriority(rawValue: 999)
constraint.isActive = true

addLabel()
}

@objc func addLabel() {
let label = UILabel()
label.backgroundColor = .gray
label.text = "Hello"
stack.addArrangedSubview(label)
}
}

最佳答案

添加这个

label.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1000), for: .vertical)

或者评论这个 block

let constraint = scrollView.heightAnchor.constraint(equalTo: stack.heightAnchor)
constraint.priority = UILayoutPriority(rawValue: 999)
constraint.isActive = true

或者让它低于 750(标签的默认垂直压缩)

constraint.priority = UILayoutPriority(rawValue: 749)

问题是标签的垂直压缩优先级低于 999 这总是使堆栈等于 ScrollView 高度,因此没有滚动

关于ios - UIScrollView 内的 UIStackView,增长到最大高度然后滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54313695/

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