在iOS 7中调整ModalViewController的大小并将其定位在中心

6
我正在尝试通过缩小宽度和高度在iPad上显示一个模态视图,但问题是它没有居中对齐。在iOS 6中它可以正常工作,但在iOS 7中它不居中对齐。
以下是我的代码:
 m_helpQA = [[HelpQAViewController alloc]init];

 m_helpQA.modalPresentationStyle = UIModalPresentationFormSheet;      

 [self presentViewController:m_helpQA animated:YES completion:NULL];
 m_helpQA.view.superview.bounds = CGRectMake(0, 0, 350, 250);//Dimensions of ModalView.

目前我是按照以下方式进行操作

这里输入图片描述

3个回答

1
你需要将框架居中,像这样应该可以工作(我还没有测试过)。
CGRect appFrame = [UIScreen mainScreen].applicationFrame;
CGRect modalFrame = m_helpQA.view.superview.frame;
modalFrame.size = CGSizeMake(350.f, 250.f);
modalFrame.origin.x = appFrame.size.width/2.0f - modalFrame.size.width/2.0f;
modalFrame.origin.y = appFrame.size.height/2.0f - modalFrame.size.height/2.0f;
m_helpQA.view.superview.frame = modalFrame;

更新代码以更正拼写错误


嗨,我试过了,它出现在中心,但我无法缩小modalView的高度。 - Ranjit

1
尝试这个针对iOS 7的方法:

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

动画看起来很丑,我建议添加动画来改善它:
[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) {
    }];
}];

同时要记住,您需要在viewDidAppear中设置navigationControllers视图的框架,以使内容大小正确。


这个问题最初是针对iOS7和大小的,但我发现它对位置(frame.origin)也很有用。然而,至少对于位置来说,在iOS8上这不再起作用了。有什么想法吗? - Chris Prince

0

请尝试以下方法:

UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
...

[self presentViewController:vc animated:NO completion:^{
    vc.view.superview.center = self.view.window.center;
}];

// resize modal view
infoModalViewController.view.superview.bounds = CGRectMake(0, 0, newWidth, newHeight);

我必须在完成块中设置中心(在这种情况下是窗口的中心,但您也可以使用父视图控制器的中心)才能使其正常工作。切换到animation:No,否则它看起来会很丑:-)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接