Can't make modal view appear right in the center of the screen on iPad iOS6

2.7k views Asked by At

I have a problem with modalviewcontrollers that I want to represent on iPad screen. If I leave the size as it is, then it's all centred fine. But I have very few info on these views, so I need to resize them.

So, when I resize them, I can't force them to appear in the middle of the screen. Even if I manage to center one of them in one orientation, it's messed up in other one.

Here is my current code:

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:acvc];
[nav setModalPresentationStyle:UIModalPresentationFormSheet];
[nav setModalTransitionStyle: UIModalTransitionStyleCoverVertical];
[[acvc view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"main_bg.jpg"]]];
[self presentViewController:nav animated:YES completion:nil];
nav.view.superview.frame = CGRectMake(self.view.bounds.size.width/2 + 175, self.view.bounds.size.height/2 - 125, 350, 250);
// nav.view.superview.center = self.view.window.center;

Would appreciate any help, thank you.

4

There are 4 answers

1
RunLoop On BEST ANSWER

Change your last line to the following:

nav.view.superview.bounds = CGRectMake(0, 0, 350, 250);
0
psy On

Here's a method that works on iOS7 as well as iOS6 and iOS5 and still allows you to use UIModalTransitionStyleCoverVertical:

-(void)presentController:(UIViewController*)controller fromRootController:(UIViewController*)rootController withSize:(CGSize)size
{
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:controller];
    nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    nav.modalPresentationStyle = UIModalPresentationFormSheet;
    [rootController presentModalViewController:nav animated:YES];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    {
        nav.view.superview.backgroundColor = [UIColor clearColor];
        nav.view.bounds = CGRectMake(0, 0, size.width, size.height);
    }
    else
    {
        nav.view.superview.bounds = CGRectMake(0, 0, size.width, size.height);
    }
}
0
poyo fever. On

in ios 7 you can use :

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    self.view.superview.bounds = CGRectMake(0, 0, width, height);
}

it works.

0
titicaca On

Stopped working in iOS7, unfortunately. Only changing ModalTransitionStyle to Dissolve solves the problem. Maybe it's a bug in iOS 7...