ios - Swift NSLayout uneven alignment -
i trying mess around autolayout programmatically in swift. have 1 problem. want view1 (red box) have regular spaced alignment superview (h:|-[view1]-|)". works right alignment not left. want left side of view have same spacing right side. view seems want align horizontally button below. know how fix or doing wrong?
override func viewdidload() { super.viewdidload() self.title = "yolo" let frame = uiscreen.mainscreen().bounds var b = uibutton.buttonwithtype(uibuttontype.system) as! uibutton b.frame = cgrectmake(frame.minx + 50, frame.miny + 100, frame.width * 0.8, frame.height * 0.2) b.backgroundcolor = uicolor.purplecolor() b.addtarget(self, action: "buttonclicked:", forcontrolevents: uicontrolevents.touchupinside) b.layer.cornerradius = 17 b.settitle("hello", forstate: uicontrolstate.normal) b.settranslatesautoresizingmaskintoconstraints(false) // additional setup after loading view, typically nib. let view1 = uiview() view1.settranslatesautoresizingmaskintoconstraints(false) view1.backgroundcolor = uicolor.redcolor() let view2 = uiview() view2.settranslatesautoresizingmaskintoconstraints(false) view2.backgroundcolor = uicolor.yellowcolor() self.view.addsubview(b) self.view.addsubview(view1) // dictionary views let viewsdictionary = ["view1":view1,"view2":view2, "b":b] let button_constraint_h:array = nslayoutconstraint.constraintswithvisualformat("h:[b(>=200)]", options: nslayoutformatoptions(0), metrics: nil, views: viewsdictionary) let button_constraint_v:array = nslayoutconstraint.constraintswithvisualformat("v:[b(>=200)]", options: nslayoutformatoptions(0), metrics: nil, views: viewsdictionary) b.addconstraints(button_constraint_h [anyobject]) b.addconstraints(button_constraint_v [anyobject]) //view1 let view1_constraint_h:array = nslayoutconstraint.constraintswithvisualformat("h:[view1(50)]", options: nslayoutformatoptions(0), metrics: nil, views: viewsdictionary) let view1_constraint_v:array = nslayoutconstraint.constraintswithvisualformat("v:[view1(50)]", options: nslayoutformatoptions(0), metrics: nil, views: viewsdictionary) view1.addconstraints(view1_constraint_h [anyobject]) view1.addconstraints(view1_constraint_v [anyobject]) let view_constraint_h:nsarray = nslayoutconstraint.constraintswithvisualformat("h:|-[view1]-|", options: nslayoutformatoptions(0), metrics: nil, views: viewsdictionary) let view_constraint_v:nsarray = nslayoutconstraint.constraintswithvisualformat("v:|-90-[view1]-[b]-0-|", options: nslayoutformatoptions.alignallleading, metrics: nil, views: viewsdictionary) let view_constraint_h2:nsarray = nslayoutconstraint.constraintswithvisualformat("h:|[b]|", options: nslayoutformatoptions(0), metrics: nil, views: viewsdictionary) view.addconstraints(view_constraint_h2 [anyobject]) view.addconstraints(view_constraint_v [anyobject]) view.addconstraints(view_constraint_h [anyobject]) }
the problem constraints make no sense. saying this:
"h:[view1(50)]" "h:|-[view1]-|"
so on 1 hand want view1
aligned margins of superview (this -
means), on other hand want view1
50 points wide (that (50)
means). can't have both!
Comments
Post a Comment