[英]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 问题
So you might want to make the whole layout contained within the stackview. 因此,您可能需要使整个布局包含在stackview中。 Just something to consider.
只是要考虑的事情。
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.
然后,您可以在代码中修改单个水平约束以更改对象的高度。
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.