简体   繁体   English

UIScrollView内部的动态UIStackView

[英]dynamic UIStackView inside of UIScrollView

Due to the extensive updates since iOS7, I wanted to ask this question because of my limited experience with autolayout and the new stackview, and I am wondering what is the best design practice to implement the following in Objective-C (not swift yet): 由于自iOS7以来进行了广泛的更新,由于我对自动布局和新的stackview的经验有限,我想问这个问题,我想知道在Objective-C中实现以下目标的最佳设计实践是什么(尚不迅速):

在此处输入图片说明

In my view, there is a container scroll view, with a child container UIView. 在我看来,有一个容器滚动视图和一个子容器UIView。 Within this UIView, there are a number of elements. 在此UIView中,有许多元素。 One of the elements is a stack of UIViews which differ in number once in a while. 元素之一是一堆UIView,其数量有时会有所不同。

This element is followed by a map and other views. 该元素后面是地图和其他视图。

This is how I plan on organizing it: 这是我计划的组织方式: 在此处输入图片说明

Questions 问题

  1. Is this the correct thing to do? 这是正确的做法吗? How would I modify the height constraint for the stackview when I remove and add elements programmatically? 以编程方式删除和添加元素时,如何修改stackview的高度限制?
  2. How do you add a subview to the UIStackView through interface builder? 如何通过接口构建器将子视图添加到UIStackView? When I do, the subview takes the size of the containing stackview. 当我这样做时,子视图采用包含的stackview的大小。

So you might want to make the whole layout contained within the stackview. 因此,您可能需要使整个布局包含在stackview中。 Just something to consider. 只是要考虑的事情。

  1. There isn't really any right way to do things. 确实没有正确的做事方法。 I would not set a height constraint on your UIStackView (do add a width constraint that's equal to the view's width). 我不会在您的UIStackView上设置高度限制(一定要添加一个等于视图宽度的宽度限制)。 Only set it on the elements you add to the stack view. 仅在添加到堆栈视图的元素上进行设置。 You will get an error, but it's just IB complaining until you add an element to your UIStackView. 您将得到一个错误,但这只是IB的抱怨,直到您向UIStackView中添加了一个元素。

To size the elements in your stackview, you have to set a horizontal constraint on them. 要调整堆栈视图中元素的大小,必须在它们上设置水平约束。 You can then modify that single horizontal constraint in code to change the height of the object. 然后,您可以在代码中修改单个水平约束以更改对象的高度。

  1. To add a subview you simply do: 要添加子视图,您只需执行以下操作:

    stackView.addArrangedSubview(childVC.view) stackView.addArrangedSubview(childVC.view)

Or in interface builder, you just drag the element into the stack view. 或在界面生成器中,您只需将元素拖到堆栈视图中即可。 Make sure it has that horizontal constraint or it will resize on you. 确保它具有水平约束,否则将根据您的大小进行调整。

Swift 4.2 斯威夫特4.2

If you want use code instead of story board, i create an example using auto layout that don't need to estimate size of content. 如果您想使用代码而不是故事板,我将使用自动布局创建一个示例,该示例无需估计内容的大小。

you just need to add to stack view or remove from it and scroll height modify automatically. 您只需要添加到堆栈视图或从堆栈视图中删除,然后滚动高度会自动修改。

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(scrollView)
        scrollView.addSubview(scrollViewContainer)
        scrollViewContainer.addArrangedSubview(redView)
        scrollViewContainer.addArrangedSubview(blueView)
        scrollViewContainer.addArrangedSubview(greenView)

        scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

        scrollViewContainer.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
        scrollViewContainer.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
        scrollViewContainer.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
        scrollViewContainer.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
        // this is important for scrolling
        scrollViewContainer.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
    }

    let scrollView: UIScrollView = {
        let scrollView = UIScrollView()

        scrollView.translatesAutoresizingMaskIntoConstraints = false
        return scrollView
    }()

    let scrollViewContainer: UIStackView = {
        let view = UIStackView()

        view.axis = .vertical
        view.spacing = 10

        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    let redView: UIView = {
        let view = UIView()
        view.heightAnchor.constraint(equalToConstant: 500).isActive = true
        view.backgroundColor = .red
        return view
    }()

    let blueView: UIView = {
        let view = UIView()
        view.heightAnchor.constraint(equalToConstant: 200).isActive = true
        view.backgroundColor = .blue
        return view
    }()

    let greenView: UIView = {
        let view = UIView()
        view.heightAnchor.constraint(equalToConstant: 1200).isActive = true
        view.backgroundColor = .green
        return view
    }()
}

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

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