FormationLayout, 另一个快速自动布局 DSL

分享于 

7分钟阅读

GitHub

  繁體
Work with auto layout and size classes easily.
  • 源代码名称:FormationLayout
  • 源代码网址:http://www.github.com/evan-liu/FormationLayout
  • FormationLayout源代码文档
  • FormationLayout源代码下载
  • Git URL:
    git://www.github.com/evan-liu/FormationLayout.git
    Git Clone代码到本地:
    git clone http://www.github.com/evan-liu/FormationLayout
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/evan-liu/FormationLayout
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    FormationLayout

    banner

    PlatformLicenseCocoaPodsCarthage

    文档

    FormationLayout 是一个 root 视图的顶层布局类。

    • FormationLayoutUIView 作为它的rootView
    • translatesAutoresizingMaskIntoConstraints 默认情况下,rootView 未设置为 false,但可以在构造函数中设置。
    • translatesAutoresizingMaskIntoConstraints subviews将自动设置为 false。
    • 没有 superView的Subviews将自动添加到 rootView
    demo { canvas, icon inFormationLayout(rootView: canvas)[icon].center(equalTo: canvas)
    }

    FormationLayout

    视图锚点:左,右,顶,底,前导,宽,宽,centerX,centerY,lastBaseline,firstBaseline,centerXWithinMargins,centerYWithinMargins

    • layout[view].anchor(equalTo: view2, multiplyBy: multiplier, plus: constant)
      • view.anchor == view2.anchor * + 常量
    • layout[view].anchor(equalTo: anchor2, of: view2, multiplyBy: multiplier, minus: constant)
      • view.anchor == view2.anchor2 * 乘法器- 常量
    • layout[view].anchor(greaterThanOrEqualTo: view2)
      • view.anchor> = view2.anchor
    • layout[view].anchor(lessThanOrEqualTo: value) ( 只有宽度和高度)
      • view.anchor <= 值
    demo { canvas, icon1, icon2, icon3 inlet layout =FormationLayout(rootView: canvas)
     layout[icon1].top(equalTo: canvas, plus: 10).leading(equalTo: canvas, plus: 20)
     layout[icon2].top(equalTo: icon1).leading(equalTo:. trailing, of: icon1, plus: 10)
     layout[icon3].bottom(equalTo: canvas, minus: 10).leading(equalTo: icon1, multiplyBy: 2)
    }

    Anchors

    帮助器

    • aspectRatio: width:height
    • 大小:宽度&高度
    • 中心:centerX & centerY
    demo { canvas, icon1, icon2, icon3 inlet layout =FormationLayout(rootView: canvas)
     layout[icon1].aspectRatio(equalTo: 1.5).width(equalTo: 30)
     layout[icon2].size(equalTo: 30).center(equalTo: canvas, multiplyBy: 0.8)
     layout[icon3].size(equalTo: icon2, minus: 10).center(equalTo: icon2, plus: 30)
    }

    Helpers

    引脚

    layout[view].pin(to: canvas) 将设置

    • view.top == canvas.top
    • view.bottom == canvas.bottom
    • view.left == canvas.left
    • view.right == canvas.right

    layout[view].pin(to: canvas, margin: 10) 将设置

    • view.top == canvas.top + 10
    • view.bottom == canvas.bottom - 10
    • view.left == canvas.left + 10
    • view.right == canvas.right - 10
    demo { canvas, icon1, icon2, icon3 inlet layout =FormationLayout(rootView: canvas)
     layout[icon1].pin(to: canvas, edgesWithMargin: [.top:10,. leading:3])
     layout[icon2].pin(to: canvas, margin: 30)
     layout[icon3].pin(to: canvas, edges: [.topBottom,. trailing], margin: 3)
    }

    Pin

    • 布局 [logo]。下面( topLayoutGuide,gap: 10 )
      • logo.top == topLayoutGuide.bottom + 10
    • 布局 [copyright]。上面( bottomLayoutGuide,gap: 10 )
      • copyright.bottom == bottomLayoutGuide.top - 10
    demo { canvas, icon1, icon2, icon3 inlet layout =FormationLayout(rootView: canvas)
     layout[icon1].center(equalTo: canvas)
     layout[icon2].before(icon1).上面(icon1, gap: 5)
     layout[icon3].after(icon1).below(icon1, gap: 5)
    }

    Abreast

    layout.group(view1, view2) 将为你创建相同的约束

    • 布局 [view1 ]
    • 布局 [view2 ]
    demo { canvas, icon1, icon2, icon3 inlet layout =FormationLayout(rootView: canvas)
     layout.group(icon1, icon2, icon3).centerY(equalTo: canvas)
     layout[icon1].leading(equalTo: canvas, plus: 5)
     layout[icon2].centerX(equalTo: canvas)
     layout[icon3].trailing(equalTo: canvas, minus: 5)
    }

    Group

    条件

    layout.when() 为一个条件创建约束,它的not 创建约束。

    demo { canvas, icon invar isLoggedIn =true 
     let layout =FormationLayout(rootView: canvas)
     let loggedInLayout = layout.when { isLoggedIn }
     layout[icon].centerX(equalTo: canvas)
     loggedInLayout[icon].top(equalTo: canvas, plus: 10)
     loggedInLayout.not[icon].bottom(equalTo: canvas, minus: 10)
     layout.update()
    }

    Condition

    isLoggedIn = false

    ConditionNot

    优先级

    默认情况下所有约束都有 UILayoutPriorityRequired。

    demo { canvas, icon inlet layout =FormationLayout(rootView: canvas)
     layout[icon].centerX(equalTo: canvas).size(equalTo: 20)
    . centerY(equalTo: canvas, at: UILayoutPriorityDefaultLow) // Try UILayoutPriorityRequired. bottom(equalTo: canvas, at: UILayoutPriorityDefaultHigh)
    }

    PriorityLow

    UILayoutPriorityRequired:

    PriorityRequired

    游乐场

    • 打开收费的文档/文档。
    • 项目导航器中打开。
    • 在每个 demo 行的最右侧单击"显示结果"按钮。

    安装

    CocoaPods

    
    pod 'FormationLayout', '~> 0.8.5'
    
    
    
    

    Carthage

    
    github"evan-liu/FormationLayout"> = 0.8.5
    
    
    
    

    AUTO  Swift  Layout  DSL  
    相关文章