[英]How to add constraints to growing dynamic views programmatically?
I have array based on its count i want to have multiple uiviews displayed next to each other on a scrollview which i should be able to scroll horizontally till the last view .How can i add constraints those views programmatically? 我有基于其计数的数组我希望在滚动视图上彼此相邻显示多个uiviews,我应该能够水平滚动到最后一个视图。如何以编程方式添加约束这些视图?
how can i add constraints to those Views so that each view will have its edges fixed to the corner of xib for device orientations? 如何为这些视图添加约束,以便每个视图的边缘都固定在xib的角落以进行设备方向?
It consists of: 它包括:
Defining the height constraints for each subview like so: 为每个子视图定义高度约束,如下所示:
V:|[subview(==scrollView)]|
The |[subview]|
|[subview]|
tells it to adjust the contentSize
to the height of the subview. 告诉它将contentSize
调整到子视图的高度。 The (==scrollView)
says that the height of the subview should be the same as the bounds
of the scroll view. (==scrollView)
表示子视图的高度应该与滚动视图的bounds
相同。 (Those two things sounds similar, but they're completely different things.) (这两件事听起来很相似,但它们完全不同。)
You want set of horizontal constraints that translate to something like: 您需要一组水平约束,转换为以下内容:
H:|[subview1(==scrollView)][subview2(==scrollView)][subview3(==scrollView)]|
So, programmatically, that looks something like: 因此,以编程方式,看起来像:
scrollView.pagingEnabled = true
var previousSubview: UIView!
var views: [String: UIView]!
for object in objects {
let subview = UIView()
subview.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollView.addSubview(subview)
views = ["subview" : subview, "scrollView" : scrollView]
if previousSubview == nil {
// it's first subview, so leading constraint is to the scroll view
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subview(==scrollView)]", options: nil, metrics: nil, views: views))
} else {
// it's not first subview, so leading constraint is to the previous subview
views["previousSubview"] = previousSubview
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[previousSubview][subview(==scrollView)]", options: nil, metrics: nil, views: views))
}
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subview(==scrollView)]|", options: nil, metrics: nil, views: views))
previousSubview = subview
}
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[previousSubview]|", options: nil, metrics: nil, views: views))
Clearly that's a Swift implementation, but the Objective-C implementation is functionally identical (same API calls, same sets of constraints). 显然,这是一个Swift实现,但Objective-C实现在功能上是相同的(相同的API调用,相同的约束集)。 But hopefully this illustrates the idea. 但希望这说明了这个想法。
Having said that, I agree with PraveenM, that a collection view is often a more prudent way to handle this sort of UI. 话虽如此,我同意PraveenM的看法,集合视图通常是处理这种UI的更谨慎的方式。 Or, if these subviews are very complicated and would benefit from having their own view controllers associated with them, a page view controller is another alternative. 或者,如果这些子视图非常复杂并且可以从与其关联的自己的视图控制器中受益,则页面视图控制器是另一种选择。 Adding all of these views to the scroll view up front is more complicated and is an extravagant use of memory. 将所有这些视图添加到前面的滚动视图更复杂,并且是对内存的过度使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.