Resize the ModalViewController and center it in iOS 7

I am trying to show modalView on an iPad, reducing its width and height, but the problem is that it does not align in the center. In iOS 6, it worked fine, but in iOS 7 it is not centered.

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. 

I am currently getting it this way

enter image description here

+6
source share
3 answers

You need to focus the frame, something like this should work (I have not 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; 

Typo correction code updated

+1
source

For iOS 7, try the following:

 [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 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 navigationControllers view frame in viewDidAppear for the correct content size.

+1
source

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 needed to set the center (in this case, the center of the window, but you can also use the center of your parent view controller) in the completion block so that it works correctly. Switch to animation:No , because otherwise it will look ugly :-)

0
source

Source: https://habr.com/ru/post/956409/