iOS-Swift-自动布局库v.Top == view.Top + 20

iOS提供了自动布局,其本质通过建立类似viewA.x = m * viewB.y + n线性关系进行布局,类的使用语法过于冗长,如:NSLayoutConstraint(item: , attribute: , relatedBy: , toItem: , attribute: , multiplier: , constant: );VFL虽然极度简洁,有些功能无法实现;目前基于开源的DSL类型自动布局库也挺多,Masonary,Snapkit;都走方法调用;这里也对自动布局做了简单封装,通过重载运算符,实现了类似:viewA.x = m * viewB.y + n的较直观的自动布局功能。示例代码如下:

func demo() {
        let v = UIView()
        v.backgroundColor = UIColor.randomColor()
        view.addSubview(v)
        
        v.Top == view.Top + 20
        v.Left == view.Left + 20
        v.Bottom == view.Bottom - 20
        v.Right == view.Right - 20
        
        let v1 = UIView()
        v1.backgroundColor = UIColor.randomColor()
        view.addSubview(v1)
        
        v1.Origin === CGPointMake(30, 30)
        v1.Size === CGSizeMake(40, 40)
        
        let v2 = UIView()
        v2.backgroundColor = UIColor.randomColor()
        v.addSubview(v2)
        v2.Inset === UIEdgeInsetsMake(50, 50, 50, 50)
        
        let v3 = UIView()
        v3.backgroundColor = UIColor.randomColor()
        v2.addSubview(v3)
        v3.Width == v2.Width * 0.2
        v3.Width == v3.Height
        v3.Center === CGPointMake(0, 0)
    }

项目已开源:HKLayoutKit

你可能感兴趣的:(iOS-Swift-自动布局库v.Top == view.Top + 20)