ios - UIButton inside UIVIew not centered in iPhone app -
i'm setting custom uiview programmatically contain several uibutton , working fine. i'm trying center uibutton inside sub-custom uiview i've created programmatically. here code i'm using
//uiview used parent blurreffectview blurredpowerdown = [[uiview alloc] initwithframe:cgrectmake(0, 0, [[uiscreen mainscreen] bounds].size.width, [[uiscreen mainscreen] bounds].size.height)]; blurredpowerdown.backgroundcolor = [uicolor clearcolor]; //i'm calling applying custom view on top of other application (yes jailbreak development issue not related @ at window = [[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]]; window.windowlevel = uiwindowlevelalert + 2; [window sethidden:no]; [window setalpha:1.0]; [window setbackgroundcolor:[uicolor clearcolor]]; [window addsubview:blurredpowerdown]; //code blur depending of behind view blur = [uiblureffect effectwithstyle:uiblureffectstyledark]; blureffectview = [[uivisualeffectview alloc]initwitheffect:blur]; blureffectview.frame = blurredpowerdown.bounds; blureffectview.autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleheight; [blureffectview setalpha:0.0]; [blurredpowerdown addsubview:blureffectview]; [uiview animatewithduration:0.5 delay:0.0 options:uiviewanimationoptioncurveeasein animations:^{ [blureffectview setalpha:1.0]; } completion:^(bool finished) { }]; //this view use center button add (in red on following picture) centerview = [[uiview alloc] init]; [centerview settranslatesautoresizingmaskintoconstraints:no]; centerview.backgroundcolor = [uicolor redcolor]; [blureffectview addsubview:centerview]; [blureffectview addconstraint:[nslayoutconstraint constraintwithitem:centerview attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:blureffectview attribute:nslayoutattributewidth multiplier:0.7 constant:0]]; [blureffectview addconstraint:[nslayoutconstraint constraintwithitem:centerview attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:blureffectview attribute:nslayoutattributeheight multiplier:0.5 constant:0]]; [blureffectview addconstraint:[nslayoutconstraint constraintwithitem:centerview attribute:nslayoutattributecenterx relatedby:nslayoutrelationequal toitem:blureffectview attribute:nslayoutattributecenterx multiplier:1.0 constant:0.0]]; [blureffectview addconstraint:[nslayoutconstraint constraintwithitem:centerview attribute:nslayoutattributecentery relatedby:nslayoutrelationequal toitem:blureffectview attribute:nslayoutattributecentery multiplier:1.0 constant:0.0]]; //and button cause me issue uibutton *cancelbutton = [uibutton buttonwithtype:uibuttontypecustom]; [cancelbutton addtarget:self action:@selector(cancelview) forcontrolevents:uicontroleventtouchupinside]; [cancelbutton settitle:@"cancel" forstate:uicontrolstatenormal]; cancelbutton.frame = cgrectmake(0, 0, 80, 80); cancelbutton.clipstobounds = yes; cancelbutton.layer.cornerradius = 80/2.0f; cancelbutton.layer.bordercolor=[uicolor whitecolor].cgcolor; cancelbutton.layer.borderwidth=2.0f; [centerview addsubview:cancelbutton]; //constraints removed per udits suggestion [centerview addconstraint:[nslayoutconstraint constraintwithitem:cancelbutton attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:centerview attribute:nslayoutattributewidth multiplier:1.0 constant:0]]; [centerview addconstraint:[nslayoutconstraint constraintwithitem:cancelbutton attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:centerview attribute:nslayoutattributeheight multiplier:1.0 constant:0]]; [centerview addconstraint:[nslayoutconstraint constraintwithitem:cancelbutton attribute:nslayoutattributecenterx relatedby:nslayoutrelationequal toitem:centerview attribute:nslayoutattributecenterx multiplier:1.0 constant:0.0]]; [centerview addconstraint:[nslayoutconstraint constraintwithitem:cancelbutton attribute:nslayoutattributecentery relatedby:nslayoutrelationequal toitem:centerview attribute:nslayoutattributecentery multiplier:1.0 constant:0.0]];
here screenshot of do
i don't understand why uibutton stay in top left (like if has no placement constraints have added them !)
i'm desperated !
thanks in advance
edit: if set cancelbutton.center = centerview.center; here get:
edit2: suggested, i've set cancelbutton.translatesautoresizingmaskintoconstraints = no; here result:
edit3: advance ^^ @udits suggestion (delete height , width constraints) button centered border strange
you should disable translation autoresizingmask constraints button:
cancelbutton.translatesautoresizingmaskintoconstraints = no;
addition: if want set width , height of button 80px, should replace 2 first constraints cancelbutton on these:
[cancelbutton addconstraint:[nslayoutconstraint constraintwithitem:cancelbutton attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1.0 constant:80]]; [cancelbutton addconstraint:[nslayoutconstraint constraintwithitem:cancelbutton attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1.0 constant:80]];
Comments
Post a Comment