How to center a subview

I am adding a subView of a UIViewController to another UIViewController.

It works great. But it's hard for me to try to center the subview in the middle of the parent view.

I read and found 2 lines of code that worked for other people but didn't work for me.

Can anyone point out my problem?

this is:

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); 

Parental 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 the operating system the CreateRoutine view controller //is becoming a child: [self addChildViewController:popupController]; //add the target frame to self view: [self.view addSubview:popupController.view]; //Tell the operating system the view controller has moved: [popupController didMoveToParentViewController:self]; } 

enter image description here

child settings enter image description hereenter image description here

+4
source share
3 answers

It looks like you are moving the view of the view controllers after centering it. Probably in didMoveToParentViewController:

Move the centering code to the end of the selectRoutine: method selectRoutine:

 - (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 the centering here popupController.view.center = [self.view convertPoint:self.view.center fromView:self.view.superview]; } 

Or better yet, move it to didMoveToParentViewController:

 - (void)didMoveToParentViewController:(UIViewController *)parent { //whatever code you have here self.view.center = self.view.superview.center; } 

You may need to modify this code a bit. But I'm sure that your problem is incorrect (in time) the placement of the centering code, which is subsequently redefined by some other positioning of the position.

+2
source

I tried this to move the spy to the center of the review.

 YourView.center = CGPointMake(self.view.center.x,self.view.center.y); 
0
source

The solutions posted here work for me. In general, if you want the counter to be between the UIAlertController message (which is usually located in the center of the UIAlertController) and any action buttons below (for example, Ok or Cancel), slightly adjust the height, for example

 spinnerIndicator.center = CGPointMake(self.superview.view.bounds.width / 2, (self.alertVC.view.bounds.height / 2) * 1.07) // The superview of the spinnerIndictator here is the UIAlertController. 

The 1.07 ratio positions the spinner only between the message and the action buttons below for all available iPhone screens based on my experiments.

0
source

Source: https://habr.com/ru/post/1479122/


All Articles