简体   繁体   中英

Error to present view controller centered in iPad iOS 6

In iOS 5 it runs correctly:

PinRequiredViewController *pinView = [[PinRequiredViewController alloc]initWithNibName:@"PinRequiredView" bundle:nil];

            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:pinView];

            // show the navigation controller modally
            navController.modalPresentationStyle = UIModalPresentationFormSheet;
            navController.modalInPopover = NO;
            navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

            [self presentViewController:navController animated:YES completion:nil];

            navController.view.superview.frame = CGRectMake(0, 0, 250, 250);

            navController.view.superview.center = self.view.window.center;

But not working fine in iOS6, the view does not stay centered in the screen, both portrait and landscape. Any solutions?

Thanks!! :)

I think it'll work if you remove the UIModalTransitionStyleFlipHorizontal transition style and use one of the other transition styles instead.

Seems like it's a bug with UIModalTransitionStyleFlipHorizontal .

Use the completion: in your presentViewController:

[self presentViewController:navController animated:YES completion:^{
        navController.view.superview.bounds = CGRectMake(0, 0, 250, 250);}];

This will make it work with UIModalTransitionStyleFlipHorizontal .

The problem is that you can set the frame of the superview to whatever you want but the origin will not be changed. That's the reason why it doesn't stay centered.

It looks like Apple restricted this on purpose in iOS6

In my understanding with UIModalTransitionStyleFlipHorizontal , the only wayout is by first presenting view without animation, setting the center point, after that in next line dismissing it and than again showing it with animated:yes. Like below.....

[self presentViewController:navController animated:NO completion:nil];

CGPoint centerPoint = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2);
navController.view.superview.center = centerPoint;
[navController dismissModalViewControllerAnimated:NO];

navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:navController animated:YES completion:nil]; 

I succeeded with the following:

aboutViewController.modalPresentationStyle = UIModalPresentationFormSheet;
aboutViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

CGRect aboutSheetFrame = aboutViewController.view.frame;
[self presentViewController:aboutViewController animated:YES completion:^{
        aboutViewController.view.superview.bounds = aboutSheetFrame;
        }];
aboutViewController.view.superview.bounds = aboutSheetFrame;

Use of UIModalTransitionStyleFlipHorizontal transition is still buggy on ios 6.1 beta 2. aboutSheetFrame is to avoid sizes hardcoding.

Just do it in viewDidAppear instead of viewDidLoad. And you're sorted !

For iOS 7 try this:

[self.navigationController presentViewController:navigationController animated:YES completion:^{
    //Make the modal bigger than normal
    navigationController.view.superview.bounds = CGRectMake(0, 0, 700, 650);
}];

The animation will look ugly so I would recommend adding an animation to improve it:

[self.navigationController presentViewController:navigationController animated:YES completion:^{
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        //Make the modal bigger than normal
        navigationController.view.superview.bounds = CGRectMake(0, 0, 700, 650);
    } completion:^(BOOL finished) {
    }];
}];

Also remember that you will need to set the frame of the navigationControllers view in the viewDidAppear for the content to be the correct size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM