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
jox
V2EX  ›  iDev

关于 Auto Layout 和 Scroll View

  •  
  •   jox · 2014-11-29 13:29:34 +08:00 · 2695 次点击
    这是一个创建于 3441 天前的主题,其中的信息可能已经有所发展或是发生改变。
    不使用auto layout的时候可以把scroll view设为root view,然后把内容直接加到scroll view里,所有的content layout结束之后更新scroll view的content size就可以了。

    使用auto layout的话好像不能这么干了,要么使用UIView作为root view,把scroll view加进去,再把content加到scroll view里,要么使用scroll view作为root view,然后得加个content wrapper,再把所有的content加到wrapper里去,总之就是多了一层,有点蛋疼,我研究半天也没整明白该怎样在不修改view hierarchy的情况下实现同样的UI效果,按照文档上的说明,我给所有的content view都设定了leading, trailing, top, bottom, width, height,照理说这样是可以计算出需要的content size的,但是使用原来的view hierarchy就是不行,因为我设定了bottom space to bottom layout guide,导致在运行的时候我设的高度直接被layout系统给丢弃了,不知道这是不是个bug。

    也就是说在不使用Auto Layout的时候view hierarchy是这样的:

    UIScrollView-|
    -------------|first content
    -------------|second content
    ....
    -------------|last content

    现在必须得使用这样的view hierarchy才能实现一样的UI:

    UIView-|
    -------|UIScrollView-|
    ---------------------|first content
    ---------------------|second content
    ....
    ---------------------|last content

    或者:

    UIScrollView-|
    -------------|Content Wrapper<UIView>-|
    --------------------------------------|first content
    --------------------------------------|second content
    ....
    --------------------------------------|last content

    有没有人知道在不改变view hierarchy的情况下如何使用auto layout实现同样的UI效果呢?
    3 条回复    2014-11-30 11:47:33 +08:00
    black
        1
    black  
       2014-11-29 13:50:58 +08:00   ❤️ 1
    文档里有说:

    The UIScrollView class scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the top, left, bottom, and right edges within a scroll view now mean the edges of its content view.

    也就是说在UIScrollView里,subview不能依赖自身相对于scroll view的约束来得到自身的size,也就导致scroll view不能基于subview来确定自身的size。

    所以,应该是只能将scroll view添加到一个view上去,然后定义scroll view的subview相对于那个view的layout constraint才能正常工作的。
    jox
        2
    jox  
    OP
       2014-11-29 14:01:27 +08:00
    @black 非常感谢

    额,this makes me sad : (
    jox
        3
    jox  
    OP
       2014-11-30 11:47:33 +08:00
    因为使用auto layout必须得调整view hierarchy才能配合scroll view使用,在有scroll view的地方我还是使用frame来布局,但是动态加载的view使用了auto layout,因为原本需要动态加载的view里面有很多控件,使用frame来布局很麻烦,需要写很多的代码来定位和调整尺寸以及间隙,使用auto layout就简单多了,这样在整体布局上因为主要就只有几个大的彼此独立的section,所以即使使用frame来布局也不会很麻烦,要想让auto layout和frame共存,trick就是对在storyboard里创建的对象使用

    setTranslatesAutoresizingMaskIntoConstraints:NO

    动态加载的view对象默认的translatesAutoresizingMaskIntoConstraints的值就是YES,所以不用管,直接设置frame的话在update constraint的过程中auto layout会自动生成需要的constraint
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1031 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:43 · PVG 07:43 · LAX 16:43 · JFK 19:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.