Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7. Change page orientation only for one view controller

I have iPhone application that supports only Portrait orientation. I want to add to my project view controller that will support only Landscape orientation? Is it possible? If yes how could I achieve that?

I have tried to crate category file like this:

@implementation UINavigationController (Rotation_IOS7)

-(BOOL)shouldAutorotate
    {

        return YES;

    }

    -(NSUInteger)supportedInterfaceOrientations
    {

      return UIInterfaceOrientationMaskLandscape;

    }

If I do this I get this error: Terminating app due to uncaught exception UIApplicationInvalidInterfaceOrientation, reason: Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES

like image 676
alexxjk Avatar asked Nov 11 '13 11:11

alexxjk


People also ask

How do I know if my device is landscape or portrait Swift?

orientation. isLandscape { print("Device is in landscape mode") } else { print("Device is in portrait mode") } } override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { determineMyDeviceOrientation() } override func didReceiveMemoryWarning() { super.

How do I set only portrait mode in Swift?

Set the supportedInterfaceOrientations property of specific UIViewControllers like this: class MyViewController: UIViewController { var orientations = UIInterfaceOrientationMask. portrait //or what orientation you want override var supportedInterfaceOrientations : UIInterfaceOrientationMask { get { return self.

Which method called when orientation changes iOS?

This inherited UIViewController method which can be overridden will be trigger every time the interface orientation will be change.


2 Answers

I've tried this and it works: http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/

First, add these code to your AppDelegat class.

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
// Get topmost/visible view controller
UIViewController *currentViewController = [self topViewController];

// Check whether it implements a dummy methods called canRotate
if ([currentViewController respondsToSelector:@selector(canRotate)]) {
    // Unlock landscape view orientations for this view controller
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

// Only allow portrait (standard behaviour)
return UIInterfaceOrientationMaskPortrait;
}

- (UIViewController*)topViewController {
  return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
  if ([rootViewController isKindOfClass:[UITabBarController class]]) {
    UITabBarController* tabBarController = (UITabBarController*)rootViewController;
    return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
  } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
    UINavigationController* navigationController = (UINavigationController*)rootViewController;
    return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
  } else if (rootViewController.presentedViewController) {
    UIViewController* presentedViewController = rootViewController.presentedViewController;
    return [self topViewControllerWithRootViewController:presentedViewController];
  } else {
    return rootViewController;
  }
}

Then, in your landscape view controller, add this method

- (void)canRotate { }
like image 159
Hanton Avatar answered Oct 21 '22 12:10

Hanton


I have search through numerous topics and finally found a working solution.

In my example, I have two VC's:

A -> VC that is embedded inside Nav. Controller and should only support Portrait view.

B -> VC that is not embedded inside a VC and should support Landscape only.

I would like to go from view A to view B (by pressing a button) and back to view then A with the specific orientations still correct.

I. Create a Category for UINavigationController and write the following in its .m file: (the code will be automatically called)

- (NSUInteger)supportedInterfaceOrientations
{
    NSLog(@"supportedInterfaceOrientations = %d ", [self.topViewController         supportedInterfaceOrientations]);

    return [self.topViewController supportedInterfaceOrientations];
}

- (BOOL)shouldAutorotate
{
    return self.topViewController.shouldAutorotate;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{
    // You do not need this method if you are not supporting earlier iOS Versions

    return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

II. Create a modal segue between A and B and after that between another one between B and A.

III. Write down in each of the View Controllers .m files the following:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

OR

- (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }

After adding this code. You will be able to change orientation for the single view B.

like image 38
Teodor Ciuraru Avatar answered Oct 21 '22 11:10

Teodor Ciuraru