Iphone Landscape mode switching to Portraite mode on loading new controller

允我心安 提交于 2019-12-04 09:30:41

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

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

}

I think you need to implement

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

in your CharacterController as well.

Charles Peterson

Found another solution:

[myCurrentViewController presentModalViewController: newController animated: NO];

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

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.

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!