Iphone Landscape mode switching to Portraite mode

2019-04-10 15:29发布

问题:

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?

回答1:

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


回答2:

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

}


回答3:

I think you need to implement

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

in your CharacterController as well.



回答4:

Found another solution:

[myCurrentViewController presentModalViewController: newController animated: NO];


回答5:

Did you have the problem where shouldAutorotateToInterfaceOrientation got called with UIInterfaceOrientationPortrait starting out even when phone was landscape?

I have an app that's primarily portrait-only and with one view controller I bring up with presentModalViewController that I do want to autorotate, which I got working well, but when I was opening it it would always start in portrait. The view controller seemed to be preferring the parent view controller's orientation over the device's.

This is what worked for me, in the modal view's view controller:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

// if device orientation is one of these, don't care what our interface orientation is
if (deviceOrientation == UIDeviceOrientationUnknown && deviceOrientation == UIDeviceOrientationFaceUp && deviceOrientation == UIDeviceOrientationFaceDown)
  return YES;

// otherwise only return YES for the ui orientation matching the current device orientation
return (interfaceOrientation == deviceOrientation);
}


回答6:

I was having the exact same problem, except i was adding the modal to a UITableViewController. I found out this was only a problem if it was called from viewDidLoad, as the modal was presented before the orientations were confirmed by the view controller's delegate.

So i just used a performSelector call with a timer, called from viewDidLoad. Now it loads in the correct orientation.



回答7:

Once again I found my own answer. LOL

After loading the new ViewController

self.view =  myCurrentViewController.view;

Update controller to orientate the new ViewController to landscape

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