ios - Make constraints for subviews programatically -
i need create uislider , put above existing slider.
i know how create constraints view if want attach it's superview:
uiview *superview = view.superview; [view setvalue: [nsnumber numberwithbool: false] forkey: @"translatesautoresizingmaskintoconstraints"]; nslayoutconstraint *topconstraint =[nslayoutconstraint constraintwithitem: view attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem: superview attribute:nslayoutattributetop multiplier:1.0 constant:0.0]; nslayoutconstraint *bottomconstraint =[nslayoutconstraint constraintwithitem: view attribute:nslayoutattributebottom relatedby:nslayoutrelationequal toitem: superview attribute:nslayoutattributebottom multiplier:1.0 constant:0.0]; nslayoutconstraint *leadingconstraint =[nslayoutconstraint constraintwithitem: view attribute:nslayoutattributeleading relatedby:nslayoutrelationequal toitem: superview attribute:nslayoutattributeleading multiplier:1.0 constant:0.0]; nslayoutconstraint *trailingconstraint =[nslayoutconstraint constraintwithitem: view attribute:nslayoutattributetrailing relatedby:nslayoutrelationequal toitem: superview attribute:nslayoutattributetrailing multiplier:1.0 constant:0.0]; nsarray *constraints = @[topconstraint, bottomconstraint, leadingconstraint, trailingconstraint]; [superview addconstraints: constraints];
but same method not work when need attach 2 subviews together. say, have view1 subview of superview. has been created time ago. need 1 (view2) have same positioning inside superview.
something like
uiview *superview = view1.superview; uiview *view2 = [[uiview alloc] init]; [superview addsubview: view2]; [view2 setvalue: [nsnumber numberwithbool: false] forkey: @"translatesautoresizingmaskintoconstraints"]; nslayoutconstraint *topconstraint =[nslayoutconstraint constraintwithitem: view1 attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem: view2 attribute:nslayoutattributetop multiplier:1.0 constant:0.0]; nslayoutconstraint *bottomconstraint =[nslayoutconstraint constraintwithitem: view1 attribute:nslayoutattributebottom relatedby:nslayoutrelationequal toitem: view2 attribute:nslayoutattributebottom multiplier:1.0 constant:0.0]; nslayoutconstraint *leadingconstraint =[nslayoutconstraint constraintwithitem: view1 attribute:nslayoutattributeleading relatedby:nslayoutrelationequal toitem: view2 attribute:nslayoutattributeleading multiplier:1.0 constant:0.0]; nslayoutconstraint *trailingconstraint =[nslayoutconstraint constraintwithitem: view1 attribute:nslayoutattributetrailing relatedby:nslayoutrelationequal toitem: view2 attribute:nslayoutattributetrailing multiplier:1.0 constant:0.0]; nsarray *constraints = @[topconstraint, bottomconstraint, leadingconstraint, trailingconstraint]; [superview addconstraints: constraints];
breaks everything. please help.
looks have backward...
you want constrain view2
to view1
:
[nslayoutconstraint constraintwithitem: view2 // <-- constrain view attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem: view1 // <-- view attribute:nslayoutattributetop multiplier:1.0 constant:0.0];
Comments
Post a Comment