iOS8 ScrollView Autolayout 怎么设置?要能滚动 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
zioc
V2EX    iDev

iOS8 ScrollView Autolayout 怎么设置?要能滚动

  •  
  •   zioc 2015-07-13 20:52:58 +08:00 4193 次点击
    这是一个创建于 3750 天前的主题,其中的信息可能已经有所发展或是发生改变。
    当前的做法是:View里面放ScrollView,ScrollView里面放一个ContainerView(UIView),ScrollView贴4边,ContainerView贴ScrollView 4边,ContainerView宽高等于View。

    当前的问题是:如果ContainerView里面还有其他View,则不能正常滚动

    谢谢!
    13 条回复    2015-07-20 21:43:49 +08:00
    sablib
        1
    sablib  
       2015-07-13 22:56:02 +08:00   1
    ContainerView宽高不应该等于View吧,它的宽高要看你想ScrollView怎么滑,
    如果想ScrollView只能上下滑,那就ContainerView宽等于View,只能左右滑的话就高等于View。
    都能滑的话ContainerView的宽高当然要大于View,因为自动布局之后Container的大小就是ScrollView的contentSize。
    simpleapples
        2
    simpleapples  
       2015-07-14 09:01:23 +08:00
    uiscrollview设置autolayout相对麻烦一些,推荐看下这篇文章:
    http://natashatherobot.com/ios-autolayout-scrollview/
    CommandZi
        3
    CommandZi  
       2015-07-14 09:38:30 +08:00
    前些天我也研究了一下,<http://chn-lyzhi.github.io/jekyll/update/2015/07/13/iOS%20开发-UIScrollView%20+%20Auto%20Layout%20+%20Size%20Classes.html>
    我们可以一起探讨下。问题可能是ScrollView无法确定ContentSize
    ashamp
        4
    ashamp  
       2015-07-14 09:39:19 +08:00   1
    1.scrollView与scrollView的superview的约束关系用来确定scrollView的frame

    2.scrollView中的『contentView』的约束关系用来确定scrollView的contentInsets

    3.contentView的宽度和高度用来确定scrollView的contentSize
    CommandZi
        5
    CommandZi  
       2015-07-14 09:40:17 +08:00
    请教正确的回帖方式~
    bawn
        6
    bawn  
       2015-07-14 10:00:50 +08:00
    yfmir
        7
    yfmir  
       2015-07-14 10:17:43 +08:00
    4楼 正解
    抱着 ‘我要怎样才能让scrollView知道我所需要的contentSize’ 这个想法去创建约束值就好了

    不过如果要兼容低版本系统活着低配置机型的话,不太建议使用autolayout来计算contentSize,特别是复杂的布局
    zioc
        8
    zioc  
    OP
       2015-07-14 10:22:40 +08:00
    @ashamp
    @yfmir
    contentView的高度怎么设置?需求是只垂直向滑动
    ashamp
        9
    ashamp  
       2015-07-14 10:32:20 +08:00
    @zioc
    1.约束contentView的宽度与scrollView相等
    2.约束contentView的高度为你所需要的高度
    之后contentView就相当于一个"画布",你的UI都可以在这里布局了
    zioc
        10
    zioc  
    OP
       2015-07-14 11:39:31 +08:00
    @ashamp

    ContentView只设置Width和Height? 要设置贴边吗?
    huanglexus
        12
    huanglexus  
       2015-07-17 11:22:02 +08:00
    我觉得 scrollview 内部直接用frame更简单,用autolayout简直就是自讨苦吃
    EggmanQ
        13
    EggmanQ  
       2015-07-20 21:43:49 +08:00
    嗯。。。刚解决这个问题。。。
    说说解决方法吧,原理楼上有了。

    我的情况是,一个 table 一个 collection 在同一个 view,view需要有拖动手势来切换,所以我用 scrollview 来包起它们了。

    然后用一个 外部‘参照View’ 来作为 content 的参照,‘参照view’的 autolayout 设置跟 scrollview (上左下右)一样。

    然后 重点就是,table 和 collection 的 autolayout 除了要确定边界之外,还需要通过 ‘参照view’ 来确定 width 和 height,从而让 scrollview 知道 content size,所以 table 和 collection 最后还需要添加 width 和 height 两个约束。

    最后三个 subview 都能正常工作了。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2270 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 01:19 PVG 09:19 LAX 18:19 JFK 21:19
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86