Resize ModalViewController and position it at center in iOS 7

我们两清 提交于 2019-12-22 05:31:16

问题


I am trying to show a modalView on an iPad by reducing its width and height, but the problem is that it is not center-aligned. In iOS 6 it used to work fine, but in iOS 7 it is not center aligned.

Below is my code:

 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.

presently I am getting it this way


回答1:


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.




回答2:


You need to center the frame something like this should work (I haven't tested).

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;

Updated Code to correct typo




回答3:


Try the following:

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

I had to set the center (in this case to the center of the window but you can use the center of your parent view controller as well) in the completition block to get it working properly. Switch to animation:No because otherwise it will look ugly :-)



来源:https://stackoverflow.com/questions/19493865/resize-modalviewcontroller-and-position-it-at-center-in-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!