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

分享 UIView 的 AutoLayout Category ,简化了我 80% 以上的 AutoLayout 代码

  •  
  •   banxi1988 ·
    banxi1988 · 2015-06-02 19:16:02 +08:00 · 3133 次点击
    这是一个创建于 3262 天前的主题,其中的信息可能已经有所发展或是发生改变。

    RT:
    对于SnapKit 中示例代码,使用我的封装,更简单,如下:

    let box = UIView()
     superview.addSubview(box)
     box.pinEdge(UIEdgeInsetsMake(20,20,20,20))
    

    不过我跟SnapKit 的定位不一样, 它尝试重新定义一种声明约束的方式,并适应所有场景.
    而我目的是,简化 80% 的写法. 其他的使用构建原生的 NSLayoutConstraint 的方式

    下面是一些用法举例:

    let box = UIView()
    box.pinCenterX() // 在父 View 水平居中
    box.pinCenterY() // 在父 View 中垂直居中
    
    let box2 = UIView()
    box2.pinCenterXToSibling(box1) // 与 box 水平居中对齐
    

    按我平常手写约束的经验来说, 简化 80% 的手写约束,是不为过的
    另: 如果某一类的约束,使用比较多,也可以快速的添加一个 convenience 方法.

    PS: 在写这些封装方法时,我发现起名字,其实是个大问题. 不想太啰嗦,又想直观.
    部分参考了 SnapKit, PureLayout的写法

    So, how do you think?

    源代码:

    https://gist.github.com/banxi1988/8b07212cadaddd28384f

    8 条回复    2015-06-03 00:04:57 +08:00
    Pixel
        1
    Pixel  
       2015-06-02 19:35:06 +08:00 via iPhone
    好像很强大
    Elethom
        2
    Elethom  
       2015-06-02 19:59:32 +08:00 via iPhone
    Masonry.
    isaced
        3
    isaced  
       2015-06-02 20:30:21 +08:00
    为什么要用 gist ?建个仓库多好。
    bengol
        4
    bengol  
       2015-06-02 20:41:30 +08:00 via iPad
    刚开始用purelayout
    banxi1988
        5
    banxi1988  
    OP
       2015-06-02 20:59:47 +08:00
    @Elethom SnapKit 算是 Masonry 的 Swift 版本.

    @isaced 这个一个文件的源代码,还是先以 gist 发布比较好.


    @Pixel 这个倒是不强大, 只是简单的封装了而已

    @bengol PureLayout 我也了解过. PureLayout 更完善些.
    likid
        6
    likid  
       2015-06-02 22:21:28 +08:00
    看起来和 Android 的 layout 的命名有点类似。
    小团队,2个人左右,全面使用 Xib 和 Storyboard
    banxi1988
        7
    banxi1988  
    OP
       2015-06-02 23:08:48 +08:00
    @likid 我也是主要使用 Xib 加 Storyboard, 但是有时,使用代码方式,感觉更好点.
    另: 一直也做 Android 开发, 所以命名也受了点影响.
    特别是 below,和above ,padding, margin 等就是受 Android 及 CSS 的影响.
    likid
        8
    likid  
       2015-06-03 00:04:57 +08:00
    @banxi1988
    最近在学 Android 开发,看起来比较像,共同学习。

    当然,有时候是用代码好点。然而如果不熟悉的话,使用了一些封装好的易用的 API,反而不懂得如果灵活的使用代码。所以我在自己的团队中更倾向于需要代码的,一定是极少的,而且尽量手写,不加入其他第三方。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1038 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 19:44 · PVG 03:44 · LAX 12:44 · JFK 15:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.