6

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

enter image description here

Ranjit
  • 4,576
  • 11
  • 62
  • 121

3 Answers3

1

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

John Wheal
  • 9,908
  • 6
  • 29
  • 39
rfrittelli
  • 1,211
  • 13
  • 15
  • hey, I tried this, its coming at center, but I am not able to reduce height of the modalView. – Ranjit Oct 22 '13 at 06:40
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.

Eeshwar
  • 277
  • 3
  • 17
  • This question was originally asked for iOS7, and about size, but I found it useful for position (frame.origin) as well. However, at least for position, this is no longer working on iOS8. Any ideas> – Chris Prince Sep 21 '14 at 06:20
0

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

bjoernb
  • 161
  • 1
  • 6