在约束的UIScrollView分页 [英] Constraints on UIScrollView with paging

查看:97
本文介绍了在约束的UIScrollView分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的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?

在此先感谢!

推荐答案

您滚动视图的子视图需要两套限制。

Your scroll view's subviews need two sets of constraints.


  • 第一组之间的约束前/后和顶/滚动视图及其子视图的底部约束。注意,不像大多数的制约,这不决定子视图的大小,而只是他们和滚动视图 contentSize

第二组是将滚动视图的上海华的子视图的宽度/高度之间的约束。这将使每个子视图为显示的全尺寸。

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.

因此​​,说明了这种编程方式:

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),它告诉子视图是主要的大小相同视图的视图控制器。请注意,您不必这样做与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.

顺便说一句,苹果讨论了技术说明TN 2154 。

By the way, Apple discusses this behavior in Technical Note TN 2154.

这篇关于在约束的UIScrollView分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆