无法使模式视图显示在 iPad iOS6 上的屏幕中央



我想在iPad屏幕上显示的modalview控制器有问题。如果我保持它的大小不变,那么它就完全居中了。但我对这些视图的信息很少,所以我需要调整它们的大小。

所以,当我调整它们的大小时,我不能强迫它们出现在屏幕中间。即使我设法把其中一个放在一个方向上,在另一个方向也会一团糟。

这是我当前的代码:

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;

非常感谢您的帮助,谢谢。

以下是一种适用于iOS7以及iOS6和iOS5的方法,并且仍然允许您使用UIModalTransitionStyleOverVertical

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

将最后一行更改为以下内容:

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

不幸的是,已停止在iOS7中工作。只有将ModalTransitionStyle更改为"溶解"才能解决问题。也许这是iOS 7中的一个错误…

在ios 7中,您可以使用:

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

它是有效的。

最新更新