Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS -- how do you control the size of a modal view controller?

I am presenting a modal view controller. If it matters, it is scrolling up from the bottom. How can I control what portion of the screen it occupies?

EDIT: I have the following in the modal view controller. It's not helping.

- (void)viewDidLoad {     TestResultView *trv = [[TestResultView alloc]initWithTest: [Model m].currentTest];     self.view = trv;     trv.frame = CGRectMake(0, 320, 320, 160);     [trv release];     [super viewDidLoad]; } 
like image 238
William Jockusch Avatar asked Nov 20 '10 03:11

William Jockusch


People also ask

What is preferredContentSize?

The preferredContentSize allows a child view controller to tell a parent container view controller the size it wants to be. In this case the parent container view is a popover presentation controller.

What is a view controller in iOS?

A view controller acts as an intermediary between the views it manages and the data of your app. The methods and properties of the UIViewController class let you manage the visual presentation of your app. When you subclass UIViewController , you add any variables you need to manage your data in your subclass.

How do I change the default view controller?

Open your storyboard. Click on the view controller corresponding to the view that you want to be the default view. Open the Attributes Inspector. Check the Is Initial View Controller check box in the View Controller section.


2 Answers

You can modify the frame of the view controller, but if you're using UIViewController's -presentModalViewController:animated: method, the view behind will be unloaded once your modal view is finished animating onto the screen (This assumes you're on an iPhone) and you'll see a white screen where your background view should be. iOS assumes that your modal view controller will be a full-screen view controller, and dumps the other view to save memory.

If you really want to show a view over part of the screen, you should instead add the UIView (no UIViewController) to your current UIViewController's view as a subview, and then animate it onscreen yourself. I think something like this would work in your UIViewController class that will present the view:

// Add the view as a subview and position it offscreen just below the current view UIView *myHalfView = [[UIView alloc] initWithFrame:someAppropriateFrame]; [self.view addSubview:myHalfView]; CGRect offScreenFrame = myHalfView.bounds; offScreenFrame.origin = CGPointMake(0.0, CGRectGetMaxY(self.view.frame));  // Now animate the view upwards [UIView beginAnimations:nil context:nil]; // Move the view upwards the height of your sliding view so it's entirely onscreen myHalfView.center = CGPointMake(myHalfView.center.x, myHalfView.center.y - myHalfView.bounds.size.height); [UIView commitAnimations]; [myHalfView release]; 

For bonus points, you could fade the view in by setting

myHalfView.alpha = 0.0; 

before the UIView animation block, and setting

myHalfView.alpha = 1.0; 

inside the block after animating the center property.

When you're done, you can do something similar but in reverse to slide the view offscreen. You can add an animationDidStop selector to the UIView animation block to be notified when the view has slid off screen so that you can remove it from the view hierarchy.

From an aesthetic point of view, you should also be careful how you do this since having a view slide up is a standard behavior, and if your view looks like a normal view but stops halfway, users may feel (even briefly) that the app has frozen. They'll figure it out, but it will leave a bad feeling about your app if not handled carefully. Mainly, I would avoid using standard full-screen cues like including a UINavigationController at the top of your view to help users understand what's going on. Half-sheets tend to be UIActionSheets on the iPhone, so think in that direction.

like image 118
atticus Avatar answered Sep 18 '22 15:09

atticus


That is nice, the above accepted answer explains a nice hack to present subViews which feel like ModalViews, but what if it is an iPad, and i can indeed give it a modalViewController which doesnt cover the entire screen.

In case of iPads, I dont think the underneath view will be unloaded. ( because there are options where we can present the modalView on iPads, which dont cover the entire screen )

ModalViewController in the end is a controller itself, and like any other controller has a root view, whose properties can be editted, if we can get hold of it.

Here is what will give you a custom frame of the ModalView :

MyViewController *viewController = [[MyViewController alloc] init]; viewConroller.modalPresentationStyle = UIModalPresentationFormSheet; [self presentModalViewController:viewController animated:YES];     //superView of viewController's view is modalViewController's view, which we were after viewController.view.superview.frame = CGRectMake(x,y,w,h);     //x y w h - can have desired values. 
like image 28
dsaw Avatar answered Sep 17 '22 15:09

dsaw