Leo
Leo

Reputation: 1567

Open Modal View with navigation controller in the centre of iPad application

I am trying to open a modal view controller in the centre of my iPad Application.

This is what I am doing in my code

Settings_iPad *vController = [[Settings_iPad alloc]
                                            initWithNibName:@"Settings_iPad" bundle:nil];

    vController.modalPresentationStyle = UIModalPresentationFormSheet;

    // Create a Navigation controller
    UINavigationController *navController = [[UINavigationController alloc]
                                             initWithRootViewController:vController];

    // show the navigation controller modally
    [self presentModalViewController:navController animated:YES];

    // Clean up resources
    [navController release];
    [vController release];

This is what I am getting http://www.use.com/48bcd41a28a13b562140

How can I open this window nicely with smaller size in the centre of the window.

Thanks

Upvotes: 2

Views: 3671

Answers (3)

Adnan Aftab
Adnan Aftab

Reputation: 14477

Here is the solution with latest iOS 7 support!

navController.modalPresentationStyle = UIModalPresentationStylePageSheet; // can be form sheet also
navController.modalTransitionStyle = UIModalTransitionStyleCrossDisolve;// in IOS 7 no other style let you resize your view controller's frame.
/* important step*/
self presentViewController:navController animated:YES completion:^{//any code you want};];// from iOS 6 onward this is supported
// now set size of the viewcontroller, if you will set before presenting it will simply ignore.
navController.view.superView.frame = CGRectMake(x,y,width,height);
navController.view.superView.center = CGPointMake (x, y);

Hope this will help you.

Upvotes: 3

aslı
aslı

Reputation: 8914

You can do something like this: After presenting the view controller modally, set its size to your desired size, and then center it.

....

navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:navController animated:YES];
//these two lines are if you want to make your view smaller than the standard modal view controller size
navController.view.superview.frame = CGRectMake(0, 0, 200, 200);
navController.view.superview.center = self.view.center;

Upvotes: 0

Mark Adams
Mark Adams

Reputation: 30846

Set the modalPresentationStyle on the navigation controller to be UIModalPresentationFormSheet and present it modally.

navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:navigationController animated:YES];

Upvotes: 4

Related Questions