ios - Using auto layout constraints programmatically create four UIView all with equal height and width -
ios - Using auto layout constraints programmatically create four UIView all with equal height and width -
how can create same view below using auto layout constraints programmatically. has been looking online resource while can't find online tutorial create , equalwidth , equalheight constraints programmatically.
please advise how can set equalwidth , equalheight programmatically accomplish layout below.
something this:
/* * ┌─┬─┐ * │1│2│ * ├─┼─┤ * │3│4│ * └─┴─┘ */ override func viewdidload() { super.viewdidload() allow view1 = uiview(frame: cgrectzero) allow view2 = uiview(frame: cgrectzero) allow view3 = uiview(frame: cgrectzero) allow view4 = uiview(frame: cgrectzero) view1.backgroundcolor = uicolor.yellowcolor() view2.backgroundcolor = uicolor.redcolor() view3.backgroundcolor = uicolor.greencolor() view4.backgroundcolor = uicolor.bluecolor() view1.settranslatesautoresizingmaskintoconstraints(false) view2.settranslatesautoresizingmaskintoconstraints(false) view3.settranslatesautoresizingmaskintoconstraints(false) view4.settranslatesautoresizingmaskintoconstraints(false) view.addsubview(view1) view.addsubview(view2) view.addsubview(view3) view.addsubview(view4) allow views = ["view1":view1, "view2":view2, "view3":view3, "view4":view4] view.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:|[view1][view2(==view1)]|", options: .allzeros, metrics: nil, views: views)) view.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:|[view3][view4(==view3)]|", options: .allzeros, metrics: nil, views: views)) view.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|[view1][view3(==view1)]|", options: .allzeros, metrics: nil, views: views)) view.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|[view2][view4(==view2)]|", options: .allzeros, metrics: nil, views: views)) // additional setup after loading view, typically nib. }
ios swift uiview autolayout
Comments
Post a Comment