Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iphone Landscape mode switching to Portraite mode on loading new controller

My app launches in landscape mode correctly and works great:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    if(interfaceOrientation == UIInterfaceOrientationPortrait)
        return NO;
    if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft )
        return YES; 
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

And updated Info.plist with

UIInterfaceOrientation = UIInterfaceOrientationLandscapeRight

Now in my main controller I switch out one ViewController for another

characterController = [ [ CharacterController alloc ] init];
myCurrentViewController = characterController;
self.view =  myCurrentViewController.view ;

and it loads but the orientation is in Portrait mode. If I then rotated the iPhone it corrects it to landscape mode. Any ideas how to keep landscape orientation when loading a new viewController into my mainController?

like image 606
Charles Peterson Avatar asked Dec 31 '22 00:12

Charles Peterson


2 Answers

Be very careful about your self.view=otherVC.view approaches. A UIViewController is intended to manage a single view. It's not designed to have its view swapped out (which is why your orientation changes aren't working). This matters in cases like -didReceiveMemoryWarning if your ViewController isn't on the screen. It will quietly dump its view, and when it comes back on screen, reload the view from the NIB (or re-run -loadView).

Your presentModalViewController: approach is somewhat better, though it's not how a modal view is built to work. It at least lets each ViewController manage its own view. Typically you would use a UITabBarController or UINavigationController here. I assume you have some reason you're avoiding these.

My recommended solution to this would be to add a UIView to your main view controller's view (as an IBOutlet or in code). You swap that view in and out rather than swapping the UIViewController's view in and out. I'd probably go ahead and subclass UIViewController to handle this, with methods modeled after UITabBarController.

@interface RNSwappableViewController : UIViewController
{
    ...
}
@property(nonatomic, assign) id<RNSwappableViewControllerDelegate> delegate;
@property(nonatomic) NSUInteger selectedIndex;
@property(nonatomic, assign) UIViewController *selectedViewController
@property(nonatomic, copy) NSArray *viewControllers;
@end

@protocol RNSwappableViewControllerDelegate : NSObject
- (void)swappableViewController:(RNSwappableViewController *)swappableViewController didSelectViewController:(UIViewController *)viewController
@end
like image 133
Rob Napier Avatar answered May 11 '23 12:05

Rob Napier


I used the recommended code. The table does, in fact, rotate. However the titlebar is still in the normal portrait orientation and position. How can I reposition the titlebar?

//Custom appear that rotates the tableview

- (void)viewDidAppear:(BOOL)animated
{
    self.view.transform = CGAffineTransformMakeRotation(-3.14 * (90) / 180.0);
    self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
    self.view.center = CGPointMake(160.0f, 240.0f);

}
like image 34
James Robins Avatar answered May 11 '23 10:05

James Robins