ios - Programatically autolayout not centering vertically -
i want use autolayout center view vertically , horizontally somehow i'm having troubles doing so. i'm running on ios8 simulator , ios8 device. there 2 versions of code have tried , none of them works. below snippets:
first let's allocate view we're using:
uiview *inputview = uiview.new; //don't hate me dot syntax :) inputview.translatesautoresizingmaskintoconstraints = no; inputview.backgroundcolor = uicolor.redcolor; [self.view addsubview:inputview];
attempt #1:
nslayoutconstraint *constraint = [nslayoutconstraint constraintwithitem:inputview attribute:nslayoutattributecenterx relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributecenterx multiplier:1.0f constant:0.0f]; //center view horizontally [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:inputview attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributewidth multiplier:1.0f constant:0.0f]; //and make have same width view [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:inputview attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:0.0f constant:260.0f]; //force 260pts view height [inputview addconstraint:constraint]; //if add self.view it's same thing constraint = [nslayoutconstraint constraintwithitem:inputview attribute:nslayoutattributecentery relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributecentery multiplier:0.5f constant:0.0f]; //and try center vertically - not work... wtf [self.view addconstraint:constraint];
seems legitimate piece of code me. nope.
attempt #2. it's same thing less lines of code.
nsarray *constraints = [nslayoutconstraint constraintswithvisualformat:@"|-[inputview(>=320)]-|" options:nslayoutformatalignallcenterx metrics:nil views:nsdictionaryofvariablebindings(inputview)]; [self.view addconstraints:constraints]; constraints = [nslayoutconstraint constraintswithvisualformat:@"v:|-[inputview(260)]-|" options:nslayoutformatalignallcentery metrics:nil views:nsdictionaryofvariablebindings(inputview)]; [self.view addconstraints:constraints];
now 1 not work either. mean center view horizontally when add vertical constraint warning telling me suck @ auto layout. says unable satisfy constraints. more specifically:
( "<nslayoutconstraint:0x17408be00 uiview:0x174197010.top == uiview:0x174196da0.topmargin>", "<nslayoutconstraint:0x17408bdb0 v:[uiview:0x174197010(260)]>", "<nslayoutconstraint:0x17408bd60 uiview:0x174196da0.bottommargin == uiview:0x174197010.bottom>", "<nslayoutconstraint:0x17008a000 'uiview-encapsulated-layout-height' v:[uiview:0x174196da0(667)]>" ) attempt recover breaking constraint <nslayoutconstraint:0x17408bd60 uiview:0x174196da0.bottommargin == uiview:0x174197010.bottom>
i'm not sure how fix this. below pictures of results attempts #1 , #2.
how can fix it?
while setting vertical constraint, made multiplier 0.5f causing this. make 1.0f had horizontal constraint.
constraint = [nslayoutconstraint constraintwithitem:inputview attribute:nslayoutattributecentery relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributecentery multiplier:1.0f constant:0.0f];