youngsoft/TangramKit

子视图联合布局

lyluoyuan opened this issue · 1 comments

我企图给定父视图的高度,两个子视图给定间隔且等高来平分剩余高度。但是貌似第一个视图必须是独立能根据自身限定确定布局的。子视图能否这样联合布局?
不知道说清楚没有,程序员还是用代码交流吧:
S = TGRelativeLayout()
S.backgroundColor = UIColor.lightGray
S.tg_width.equal(100)
S.tg_height.equal(400)
S.tg_top.equal(64)
view.addSubview(S)
A = UIView()
C = UIView()
C.backgroundColor = UIColor.blue
A.backgroundColor = UIColor.red
A.tg_left.equal(0).and().tg_right.equal(0).and().tg_top.equal(10)
//A没有给定高度,和C等高来平分S剩余空间
C.tg_left.equal(0).and().tg_right.equal(0).and().tg_top.equal(A.tg_bottom).offset(10).and().tg_bottom.equal(10)
C.tg_height.equal(A.tg_height)//A和C等高
S.addSubview(A)
S.addSubview(C)

您好,您的需求是A和C均分S的高度,而且A和S,A和C,以及S和C之间要有10的间隔对吧。上面的代码您写复杂了。

这个需求你可以用线性布局来实现
        let  S = TGLinearLayout(.vert)
        S.backgroundColor = UIColor.lightGray
        S.tg_width.equal(100)
        S.tg_height.equal(400)
        S.tg_top.equal(64)
        S.tg_topPadding = 10  //子视图顶部边距为10
        S.tg_bottomPadding = 10  //子视图顶部边距为10
        S.tg_vspace = 10   //子视图之间的间距为10
        view.addSubview(S)
        let  A = UIView()
        let  C = UIView()
        C.backgroundColor = UIColor.blue
        A.backgroundColor = UIColor.red
        A.tg_width.equal(.fill).and().tg_height.equal(.fill)   //高度和宽度都是填充或者A.tg_size(width:.fill, height:.fill)
        C.tg_width.equal(.fill).and().tg_height.equal(.fill)
        S.addSubview(A)
        S.addSubview(C)
        
或者线性布局更加简单来实现
       let  S = TGLinearLayout(.vert)
        S.backgroundColor = UIColor.lightGray
        S.tg_width.equal(100)
        S.tg_height.equal(400)
        S.tg_top.equal(64)
        S.tg_topPadding = 10  //子视图顶部边距为10
        S.tg_bottomPadding = 10  //子视图顶部边距为10
        S.tg_vspace = 10   //子视图之间的间距为10
        S.tg_gravity = .fill  //里面的子视图宽度和高度都填充。
        view.addSubview(S)
        let  A = UIView()
        let  C = UIView()
        C.backgroundColor = UIColor.blue
        A.backgroundColor = UIColor.red
        S.addSubview(A)
        S.addSubview(C)
这个需求你可以用相对布局来实现(具体例子见相对布局第2个例子)
        let  S = TGRelativeLayout()
        S.backgroundColor = UIColor.lightGray
        S.tg_width.equal(100)
        S.tg_height.equal(400)
        S.tg_top.equal(64)
        S.tg_topPadding = 10  //子视图顶部边距为10
        S.tg_bottomPadding = 10  //子视图顶部边距为10
        view.addSubview(S)
         let A = UIView()
         let C = UIView()
        C.backgroundColor = UIColor.blue
        A.backgroundColor = UIColor.red
        A.tg_width.equal(.fill)
        C.tg_width.equal(.fill)
        
        A.tg_height.equal([C.tg_height.add(-5)], increment:-5)   //这里采用数组的方式来进行高度均分,都减去5来保留10的子视图间隙
        C.tg_top.equal(A.tg_bottom, offset:10)
        
        S.addSubview(A)
        S.addSubview(C)