D
D
deleted-mezhevikin2013-11-04 12:33:23
iOS
deleted-mezhevikin, 2013-11-04 12:33:23

Screen orientation problem?

02656d05d4e5333574ee7a5c6610f8c9.png
There are two controllers FirstViewController and SecondViewController they are in NavigationController.
It is necessary that the FirstViewController be only in portrait orientation, and the SecondViewController in all.
Tried like this:
In NavigationController added

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

And in SecondViewController
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

in plist
<key>UISupportedInterfaceOrientations</key>
  <array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
  </array>

What am I missing?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
deleted-mezhevikin, 2013-11-04
@deleted-mezhevikin

As timokhin said, orientation information is taken from the root controller, if your root is navigationController, then we create a class and inherit it from UINavigationController.
We add three methods that take the value from the top controller:

//
//  NavigationController.h
//

#import <UIKit/UIKit.h>

@interface NavigationController : UINavigationController

@end

//
//  NavigationController.m
//

#import "NavigationController.h"

@implementation NavigationController

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

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

@end

now the methods will start working in the view controllers themselves
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return YES;
}

For example, I have a custom tabbarcontroller at the root, and there are already controllers wrapped in UINavigationController in it, I added methods:
- (BOOL)shouldAutorotate
{
    UIViewController *viewController = [(UINavigationController *)self.selectedViewController topViewController];
    return [viewController shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    UIViewController *viewController = [(UINavigationController *)self.selectedViewController topViewController];
    return [viewController  supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
     UIViewController *viewController = [(UINavigationController *)self.selectedViewController topViewController];
    return [viewController preferredInterfaceOrientationForPresentation];
}

with the standard UITabBarController I think it will be the same

M
Maxim Timokhin, 2013-11-04
@timokhin

I haven't tried it myself, but judging by the documentation, these methods will only work if:

-(BOOL)shouldAutorotate{
    return YES;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question