Jonathan Thurft
Jonathan Thurft

Reputation: 4173

How to center a subview

I am adding a UIViewController subView to another UIViewController.

It works great. But I am having a hard time trying to center the subview in the middle of the parent view.

I read up and I found 2 lines of code that worked for other people but its not working for me.

Could anyone point out my 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 the operating system the CreateRoutine view controller
    //is becoming a child:
    [self addChildViewController:popupController];

    //add the target frame to self's 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 here enter image description here

Upvotes: 4

Views: 4988

Answers (3)

Bhupendrasingh Lohar
Bhupendrasingh Lohar

Reputation: 173

For moving a subview to the center of view i tried this

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

Upvotes: 1

sysuser
sysuser

Reputation: 1100

The solutions posted here works for me. In general if you want the spinner to come between the UIAlertController message (which generally is at the center of the UIAlertController) and any action buttons below (like Ok or cancel), slightly adjust it's height like

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 ratio 1.07 positions the spinner just between the message and action buttons beneath for all available iPhone screens based on my experimentation.

Upvotes: 0

Rok Jarc
Rok Jarc

Reputation: 18875

Seems like you reposition your view controllers view after centering it. Probably in didMoveToParentViewController:.

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

}

Or better yet, move it to the didMoveToParentViewController:

- (void)didMoveToParentViewController:(UIViewController *)parent
{
    //whatever code you have here

    self.view.center = self.view.superview.center;
}

Possibly you will have to modify this code a bit. But i'm certain that your problem is incorrect (execution-time-wise) placement of the centering code - that gets subsequently overriden by some other view-positioning.

Upvotes: 2

Related Questions