ios - How to center a subview -
i adding uiviewcontroller subview uiviewcontroller.
it works great. having hard time trying center subview in middle of parent view.
i read , found 2 lines of code worked other people not working me.
could point out problem??
those are:
popupcontroller.view.center = [self.view convertpoint:self.view.center fromview:self.view.superview];
and
popupcontroller.view.center = cgpointmake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
parent view controller code:
- (ibaction)selectroutine:(id)sender { uistoryboard *storyboard = [uistoryboard storyboardwithname:@"storyboard" bundle:nil]; createroutinepopupviewcontroller* popupcontroller = [storyboard instantiateviewcontrollerwithidentifier:@"createroutinepopupview"]; // popupcontroller.view.center = [self.view convertpoint:self.view.center fromview:self.view.superview]; popupcontroller.view.center = cgpointmake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2); //tell operating system createroutine view controller //is becoming child: [self addchildviewcontroller:popupcontroller]; //add target frame self's view: [self.view addsubview:popupcontroller.view]; //tell operating system view controller has moved: [popupcontroller didmovetoparentviewcontroller:self]; }
child settings
seems reposition view controllers view after centering it. in didmovetoparentviewcontroller:
.
move centering code end of selectroutine:
method
- (ibaction)selectroutine:(id)sender { uistoryboard *storyboard = [uistoryboard storyboardwithname:@"storyboard" bundle:nil]; createroutinepopupviewcontroller* popupcontroller = [storyboard instantiateviewcontrollerwithidentifier:@"createroutinepopupview"]; [self addchildviewcontroller:popupcontroller]; [self.view addsubview:popupcontroller.view]; [popupcontroller didmovetoparentviewcontroller:self]; //do centering here popupcontroller.view.center = [self.view convertpoint:self.view.center fromview:self.view.superview]; }
or better yet, move didmovetoparentviewcontroller:
- (void)didmovetoparentviewcontroller:(uiviewcontroller *)parent { //whatever code have here self.view.center = self.view.superview.center; }
possibly have modify code bit. i'm problem incorrect (execution-time-wise) placement of centering code - gets subsequently overriden other view-positioning.
Comments
Post a Comment