UIScrollView与分页的约束 [英] Constraints on UIScrollView with paging
问题描述
我有 UIScrollView
它覆盖整个屏幕,在它里面我有3 UIView
侧,用分页管理。我想制作正确的约束,所以它也适合iPhone 6。
I have UIScrollView
which covers the whole screen, and inside it I have 3 UIView
objects side-by-side, managed with paging. I'd like to make the right constraints so it would fit iPhone 6 as well.
在拖动时它看起来像这样:
It looks like this when dragging:
UIScrollView
正在工作,但是如何安排在 UIScrollView
?
The constraints of the UIScrollView
are working well, but how do I arrange the constraints of the views inside the UIScrollView
?
中的意见的约束感谢提前! !
thanks in Advance!!
推荐答案
您的滚动视图的子视图需要两组约束。
Your scroll view's subviews need two sets of constraints.
-
第一组是滚动视图的前/后和上/下约束之间的约束及其子视图。注意,与大多数约束不同,这不是指定子视图的大小,而是它们和滚动视图的
contentSize
之间的关系。
第二组是在子视图的宽度/高度到滚动视图的 superview 之间的约束。这将使每个子视图都是显示的完整大小。
The second set is the constraints between the width/height of the subviews to the scroll view's superview. This will make each of the subviews to be the full size of the display.
/ p>
Thus, illustrating this programmatically:
let scrollView = UIScrollView()
scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollView.pagingEnabled = true
view.addSubview(scrollView)
let redView = UIView()
redView.backgroundColor = UIColor.redColor()
redView.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollView.addSubview(redView)
let greenView = UIView()
greenView.backgroundColor = UIColor.greenColor()
greenView.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollView.addSubview(greenView)
let blueView = UIView()
blueView.backgroundColor = UIColor.blueColor()
blueView.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollView.addSubview(blueView)
let views = [
"scrollView" : scrollView,
"blueView" : blueView,
"greenView" : greenView,
"redView" : redView,
"view" : view
]
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: nil, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|", options: nil, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[redView(==view)]|", options: nil, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[greenView(==view)]|", options: nil, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[blueView(==view)]|", options: nil, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[redView(==view)][greenView(==view)][blueView(==view)]|", options: nil, metrics: nil, views: views))
以可视格式语言(VFL)引用(== view)
,它告诉子视图与视图控制器的主视图大小相同。注意,你不必用VFL(你也可以在IB中做到这一点),但它只是一个方便的方式来表达约束。
The salient detail here is the reference to (==view)
in the visual format language (VFL), which tells the subviews to be the same size of the main view of the view controller. Note, you don't have to do this with VFL (you can do it in IB, too), but it's just a convenient way to articulate the constraints.
方法,Apple在技术说明TN 2154 中讨论此行为。
By the way, Apple discusses this behavior in Technical Note TN 2154.
这篇关于UIScrollView与分页的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!