UIScrollView的寻呼自动布局和放大器;故事板 [英] UIScrollView Paging Autolayout & Storyboard
问题描述
有很多关于与自动布局和充足的滚动视图左右滚动分页意见的答案,但我不能找到既解决了单一的东西。
There are plenty of answers regarding scroll views with autolayout and plenty about scrollview paging, but I can't find a single thing that addresses both.
我不想做任何花哨的......只是 7
全屏图像的意见,我想分页水平滚动,但为求简单(哈哈!),我决定尝试它脚本的所有权利。
I'm not trying to do anything fancy...just 7
full-screen image views that I would like to scroll horizontally with paging, but for the sake of simplicity (ha!), I decided to attempt it all right in the storyboard.
控制器设置为自由大小的宽度2240(320 * 7)
。然后,我将它设置苹果提出了自动布局...
The controller is set to freeform size with a width of 2240 (320*7)
. I then set it up the way Apple suggests for autolayout...
UIScrollview
/-----UIView
/----------Content (7 image views)
滚动视图具有 0/0/0/0
约束所有的边一样,里面的UIView。
The scrollview has 0/0/0/0
constraints to all edges, as does the UIView inside.
分页时启用关闭时,它的行为美丽 - 这正是预期。但是,一旦我把寻呼上,刷卡使视图发疯,滚动整个 2240
宽度,然后反弹并最终降落在正确的页面上。
When Paging Enabled is off, it behaves beautifully - exactly as expected. But once I turn Paging on, a swipe makes the view go crazy, scrolling the entire 2240
width, and then bouncing back and eventually landing on the proper page.
我知道我刚杀了一切,编程做了尝试和真正的选择,但我固执想弄清楚这一点。它必须是可能的!
I know I have the tried-and-true option of just scrapping it all and doing it programmatically, but my stubbornness wants to figure this out. It must be possible!
推荐答案
我有分页和自动版式的工作完全没有一个UIScrollView。这里是我的设置:
I have a UIScrollView with paging and AutoLayout working perfectly fine. Here is my set up:
UIView // Main view
|---> Dummy UIView // See below
|---> UIScrollView
|---> Content UIView
|---> Page 1 Container
|---> Page 2 Container
我使用的约束虚拟的UIView - >家长的UIView
是我想做的事情分页滚动视图的大小要和的UIScrollView - >虚拟的UIView
是(0,0,0,0)在各方面。这仅仅是常规的自动布局的东西它创建了一个虚拟的UIView,我想把滚动视图和的UIScrollView
这完全填充假的UIView
。
The constraints I used are
Dummy UIView -> Parent UIView
is whatever I want the size of the paging scrollview to be, and UIScrollView -> Dummy UIView
is (0,0,0,0) on all sides. This is just regular auto layout stuff which creates a dummy UIView where I want to put the scrollview and a UIScrollView
which completely fills the dummy UIView
.
请参阅从苹果公司的技术说明的自动版式和 UIScrollViews
:<一href=\"https://developer.apple.com/library/ios/technotes/tn2154/_index.html\">https://developer.apple.com/library/ios/technotes/tn2154/_index.html
Refer to the Technote from Apple for AutoLayout and UIScrollViews
: https://developer.apple.com/library/ios/technotes/tn2154/_index.html
的滚动视图里面的内容都必须有一个内在的大小。它不能依靠滚动视图来获得它的大小。的
由于在技术说明指出,从四面设置约束查看内容
到的UIScrollView
来(0,0,0,0)。确切的值并不真正的问题,因为你是在告诉的UIScrollView
的是,这是获得视图的 contentSize
从
As indicated in the TechNote, set the constraints from all four sides of the Content View
to the UIScrollView
to (0,0,0,0). The exact values don't really matter since all you are telling the UIScrollView
is that this is the view to get the contentSize
from.
在这点X code会抱怨说查看内容
没有内在的大小。这里是绝招:这是我们使用虚拟的UIView
,我们在上面创建的。在虚拟的UIView
的大小是precisely每个页面中的的UIScrollView
的大小。
At this point Xcode will complain that Content View
has no intrinsic size. And here is the trick: This is where we use the Dummy UIView
that we created above. The size of the Dummy UIView
is precisely the size of each page in the UIScrollView
.
所以我们设置内容的UIView的高度
等于虚拟的UIView的高度
和宽度的内容的UIView
等于页面倍虚拟的UIView
使用自动版式的宽度的数量。 (对于后来改变约束中的乘数是的页数)。
So we set the height of Content UIView
equal to height of Dummy UIView
and the width of the Content UIView
equal to the number of pages times the width of the Dummy UIView
using AutoLayout. (For the later change the multiplier in the constraint to be the number of pages).
现在创建内容的UIView
,你通常会并设置寻呼已启用
内页为是你的的UIScrollView
,瞧你在寻呼的UIScrollView
使用自动版式。
Now create pages inside the Content UIView
as you normally would and set Paging Enabled
to yes on your UIScrollView
and voila you have paging in a UIScrollView
using AutoLayout.
我在IOS 6,7和测试这一点; 8。
I've tested this in IOS 6, 7 & 8.
更新:
下面是与此设置一个示例项目的要求:
https://github.com/kostub/PagingScrollView
Here is a sample project with this setup as requested: https://github.com/kostub/PagingScrollView
这篇关于UIScrollView的寻呼自动布局和放大器;故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!