繁体   English   中英

如何增加包含UIStackView的UIView高度

[英]How to increase UIView height which contains UIStackView

我有一个包含标签的自定义视图,标签可以有多个行文本。 所以我已经在UIStackView中添加了该标签,现在我的StackView高度正在增加,但是自定义视图高度不会增加。 我没有在我的StackView上添加底部约束。 我应该怎么做,以使我的CustomView高度也随StackView增加。

 let myView = Bundle.main.loadNibNamed("TestView", owner: nil, options: nil)![0] as! TestView
    myView.lbl.text = "sdvhjvhsdjkvhsjkdvhsjdvhsdjkvhsdjkvhsdjkvhsjdvhsjdvhsjdvhsjdvhsjdvhsjdvhsjdvhsdjvhsdjvhsdjvhsdjvhsdjvhsjdvhsdjvhsdjvhsjdvhsdjvhsjdvhsdjvhsdjvhsdjvhsjdv"
    myView.lbl.sizeToFit()
    myView.frame = CGRect(x: 10, y: 100, width: UIScreen.main.bounds.width, height: myView.frame.size.height)
    myView.setNeedsLayout()
    myView.layoutIfNeeded()

    self.view.addSubview(myView)

我想根据我的stackview高度增加自定义视图的高度。 请帮忙。

superview的stackView约束示例。 同样,superview对其高度不应有任何限制。

您应该将自定义视图的顶部和底部锚点限制为stackview的顶部和底部锚点。 随着stackView的增长,它将推动该底部边距。 这是一个编程示例:

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {

    private lazy var stackView = UIStackView()
    private lazy var addLabelButton = UIButton(type: .system)

    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let stackViewContainer = UIView(frame: view.bounds)
       stackViewContainer.backgroundColor = .yellow
        stackViewContainer.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stackViewContainer)
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.axis = .vertical

        addLabelButton.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(addLabelButton)
        stackViewContainer.addSubview(stackView)

        NSLayoutConstraint.activate([
            // Container constrained to three edges of its superview (fourth edge will grow as the stackview grows
            stackViewContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            stackViewContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            stackViewContainer.topAnchor.constraint(equalTo: view.topAnchor),

            // stackView constraints - stackView is constrained to the
            // for corners of its contaier, with margins
            {
                // Stackview has a height of 0 when no arranged subviews have been added.
                let heightConstraint = stackView.heightAnchor.constraint(equalToConstant: 0)
                heightConstraint.priority = .defaultLow
                return heightConstraint
            }(),
            stackView.topAnchor.constraint(equalTo: stackViewContainer.topAnchor, constant: 8),
            stackView.leadingAnchor.constraint(equalTo: stackViewContainer.leadingAnchor, constant: 8),
            stackView.trailingAnchor.constraint(equalTo: stackViewContainer.trailingAnchor, constant: -8),
            stackView.bottomAnchor.constraint(equalTo: stackViewContainer.bottomAnchor, constant: -8),

            // button constraints
            addLabelButton.topAnchor.constraint(equalTo: stackViewContainer.bottomAnchor, constant: 8),
            addLabelButton.centerXAnchor.constraint(equalTo: stackViewContainer.centerXAnchor)
            ])

        addLabelButton.setTitle("New Label", for: .normal)
        addLabelButton.addTarget(self, action: #selector(addLabel(sender:)), for: .touchUpInside)
        self.view = view
    }

    private(set) var labelCount = 0

    @objc func addLabel(sender: AnyObject?) {
        let label = UILabel()
        label.text = "Label #\(labelCount)"
        labelCount += 1
        stackView.addArrangedSubview(label)
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

请注意,当UIStackView为空时,其高度定义不正确。 这就是为什么我将其heightAnchor约束设置为0且优先级较低的原因。

首先,您应该在UIS​​tackView上添加底部约束。 这将有助于自动布局确定UIStackView的运行时大小。

现在创建您的自定义UIView的实例,但不要设置它的框架并将其添加到UIStackView。 确保自定义UiView已为自动布局设置了所有约束,以确定其运行时限。 基于UIView元素的内容,这将增加UIView和UIStackView的高度。

有关更多详细信息,您可以在https://stackoverflow.com/a/57954517/3339966上关注我的详细解答。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM