Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modal View Controller Won't Start in Landscape Mode

I have a navigation based app that has a detail view (UIWebView) with action buttons across the bottom in a UIToolbar. I want to add 'notes' when the 'notes' button is pushed. Everything works fine when the webview is in portrait mode. I press the notes button, the modal view opens fine and works great.

The problem occurs when the webview is in landscape mode. If I press the notes button, all the code to open the modal view gets called but all I get is a white screen. One comment: If I open the modal view in portrait and then rotate the device, it rotates fine into landscape mode. It just won't open correctly in landscape mode.

I have another button that brings up the mail composer which has the identical behavior. Here is the code in my UIWebViewController:

- (IBAction)addNotes:(id)sender 
{
    NotesViewController *notesViewController;

    // create the view controller and set it as the root view of a new navigation
    // controller

    notesViewController = [[NotesViewController alloc] initWithPrimaryKey:self.record.primaryKey];
    UINavigationController *newNavigationController =  
    [[UINavigationController alloc] initWithRootViewController:notesViewController];

    // present the navigation controller modally

    [self presentModalViewController:newNavigationController animated:YES];
    [notesViewController release]; 
    [self.view setNeedsDisplay]; // not sure if I need this!  I was trying different things...
    [self.devotionText setNeedsDisplay]; // ditto...
    [newNavigationController release];
}

Any ideas? I've tried all sorts of different things to no avail. I just get a white screen with no navigation bar (although there is a status bar at the top).

like image 521
Glasswing Avatar asked Sep 22 '10 17:09

Glasswing


4 Answers

Modals don't always get information about rotations, and they get their info from the status bar, which doesn't always work right. Put this in your viewWillAppear to fix: [UIApplication sharedApplication].statusBarOrientation = self.interfaceOrientation And, if you want a navigation controller inside your modal, you need to create one.

Also, you don't need the setNeedsDisplay. That only effects the current views, not the modal you are presenting.

like image 180
MishieMoo Avatar answered Sep 29 '22 09:09

MishieMoo


Answer is here:

https://stackoverflow.com/a/10250747/1449618

Use the window's root view controller to present:

[self.view.window.rootViewController presentViewController:masterView
                                                  animated:YES
                                                completion:NULL];
like image 43
Jimmy_m Avatar answered Sep 29 '22 09:09

Jimmy_m


Wow, I lost days over that issue ... but I found a solution!

I had the same problem you had: the method "presentModalViewController:animated:" only worked in portrait mode.

After a lot of trial and error, I found out that the reason was that I had several view controllers active at the same time. I implemented a navigation system which switched between different view controllers, with one parent handling the children. (I could not use UINavigationController, because I needed a different look.)

So, my root view controller had a root view object, and several child view controllers. When a child view controller was activated, its view object was added as subview to the view of the root view controller.

The "presentModalViewController" method didn't like that. However, as soon as I set the "parentViewController" property of the child view controllers, it worked!

The problem is only that "parentViewController" is a read-only property. You have to extend the UIViewController class so you can access it.

@interface UIViewController (HelperExtension)

@property (nonatomic, assign) UIViewController *parent;

@end

@implementation UIViewController (HelperExtension)

- (UIViewController *)parent
{
    return self.parentViewController;
}

- (void)setParent:(UIViewController *)parent
{
    [self setValue:parent forKey:@"_parentViewController"];
}

@end

So, whenever you add the view of a child view controller to your parent view controller, call the "setParent:" method after doing it. Then it will work!

like image 24
PrimaryFeather Avatar answered Sep 29 '22 08:09

PrimaryFeather


Got the same issue when presenting modally a navigation controller. Be sure to have correctly implement : shouldAutorotateToInterfaceOrientation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    BOOL shouldAutorotate = NO;
    if( isControllerMangingAllOrientations )
    {
        shouldAutorotate = YES;
    }
    else
    {
        shouldAutorotate = (toInterfaceOrientation == UIInterfaceOrientationPortrait);
    }
    return shouldAutorotate;
}

I was setting the boolean in the viewDidLoad method, not a good idea. Putting it in the initWithNibName:bundle: method is the right place.

like image 37
ıɾuǝʞ Avatar answered Sep 29 '22 07:09

ıɾuǝʞ