LeoMaer LeoMaer - 1 year ago 123
iOS Question

iOS:How to let a `uiview` support portrait and landscape orientation?

I have a

which is inherited

@interface SDPhotoBrowser : UIView <UIScrollViewDelegate>

But now I have a requirement to let the

portrait and landscape orientation, and how to do this ?

my photobroser

EDIT:In my application, my app only support
orientation, only in my
which is
, I want to support
portrait and landscape

EDIT:I apply
's answer, and his answer is very good, in the actually coding, I suggest a function to change
portrait and landscape orientation

//(force landscape:[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];)
//(force Portrait:[self interfaceOrientation:UIInterfaceOrientationPortrait];)
+ (void)interfaceOrientation:(UIInterfaceOrientation)orientation
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
// from 2
[invocation setArgument:&val atIndex:2];
[invocation invoke];

This function maybe helpful for perfect
support portrait and landscape orientation
in code.

Answer Source

Now I know your app only support portrait orientation. It will take your two step to support portrait and landscape orientation in special view controller.
I suppose you using Objective-C

1. add a property in AppDelegate.h named @property (nonatomic, assign) BOOL shouldRotation;
2. In 'AppDelegate.m', implement orientation method of UIApplicationDelegate - - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window, just like

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.shouldRotation) {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
    else {
        return UIInterfaceOrientationMaskPortrait;

Now you can support landscape orientation in any UIViewController you want.

- (void)viewDidLoad {
    [super viewDidLoad];

    // enable landscape orientation
    [UIApplication sharedApplication].delegate.shouldRoration = YES;

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    // disable landscape orientation
    [UIApplication sharedApplication].delegate.shouldRoration = NO;

May it be helpful.
PS, there is another way to solve it:

  1. You app support all orientation
  2. All of your UIViewController override - (UIInterfaceOrientationMask)supportedInterfaceOrientations, return UIInterfaceOrientationMaskPortrait or UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download