【问题标题】:How to increase UIView height which contains UIStackView如何增加包含 UIStackView 的 UIView 高度
【发布时间】:2019-09-16 18:14:34
【问题描述】:

我有一个包含标签的自定义视图,标签可以有多行文本。所以我在 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 高度增加我的自定义视图高度。 请帮忙。

【问题讨论】:

    标签: ios uistackview


    【解决方案1】:

    stackView 约束及其父视图的示例。 superview 的高度也不应该有限制。

    【讨论】:

    • 制作约束 >= 为我完成了这项工作。为什么这是必要的?
    【解决方案2】:

    您应该将自定义视图的顶部和底部锚点设置为约束到堆栈视图的顶部和底部锚点。随着 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 且优先级较低。

    【讨论】:

      【解决方案3】:

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

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

      欲知更多详情,您可以在https://stackoverflow.com/a/57954517/3339966关注我对此的详细回答

      【讨论】:

        猜你喜欢
        • 2021-06-13
        • 2019-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-15
        • 1970-01-01
        • 2013-05-09
        相关资源
        最近更新 更多