WCLayout 0.0.8

WCLayout 0.0.8

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jul 2015
SPMSupports SPM

Maintained by HaloWang.



WCLayout 0.0.8

  • By
  • 王策

假设我们想达到这样的需求:

1

1、testView_A
    距离顶部30,距离右侧30,高度为100,距离左侧30
2、testView_B
    距离 testView_A 的底部 25
    距离 superView 左侧的距离等于 testView_A 距离 superView 左侧的距离
    距离 superView 右侧的距离等于 testView_A 距离 superView 右侧的距离 + 10
    距离 superView 底部的距离等于 30

不使用任何第三方布局方式:

class ViewController: UIViewController {

    let testView_A = UILabel()

    let testView_B = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(testView_A)
        view.addSubview(testView_B)

        testView_A.text = "testView_A"
        testView_B.text = "testView_B"

        var frameA = CGRectZero
        frameA.origin.x = 30
        frameA.origin.y = 30
        frameA.size.width = UIScreen.mainScreen().bounds.size.width - frameA.origin.x - 30
        frameA.size.height = 100
        testView_A.frame = frameA

        var frameB = CGRectZero
        frameB.origin.x = testView_A.frame.origin.x
        frameB.origin.y = testView_A.frame.origin.y + testView_A.frame.size.height + 25
        frameB.size.width = UIScreen.mainScreen().bounds.size.width - testView_A.frame.origin.x - (testView_A.superview!.frame.size.width - (testView_A.frame.origin.x + testView_A.frame.size.width)) - 10
        frameB.size.height = UIScreen.mainScreen().bounds.size.height - frameB.origin.y - 30
        testView_B.frame = frameB


        testView_A.backgroundColor = UIColor.redColor()
        testView_B.backgroundColor = UIColor.blueColor()
    }
}

使用WCLayout:

import UIKit
import WCLayout

class ViewController: UIViewController {

    let testView_A = UILabel()

    let testView_B = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(testView_A)
        view.addSubview(testView_B)

        testView_A.text = "testView_A"
        testView_B.text = "testView_B"

        testView_A
            .top(30)
            .right(30)
            .height(100)
            .left(30)

        testView_B
            .top(testView_A.chainBottom + 25)
            .left(testView_A.left)
            .right(testView_A.right + 10)
            .bottom(30)

        testView_A.backgroundColor = UIColor.redColor()
        testView_B.backgroundColor = UIColor.blueColor()
    }
}