iOS - Lock a specific UIViewController to a specific orientation [closed]

☆樱花仙子☆ 提交于 2019-12-18 08:57:23

问题


My application supports every 4 orientation, I have a UIViewController which is in LandscapeRight. I am using UINavigationController to push that UIViewController, i want that UIViewController to be only in UIInterfaceOrientationLandscapeRight but when I rotate the phone, it switches back to other Orientation.

-(BOOL)shouldAutorotate{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationLandscapeRight;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeRight;
}

回答1:


just remove those shouldAutorotate, supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation.

and add this to the viewcontroller you want to display landscape only.

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[UIDevice currentDevice] setValue:
     [NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft]
                                forKey:@"orientation"];
} 

actually, this is from a similar question with solution here. How to force view controller orientation in iOS 8?




回答2:


You need to create a sub class of UIViewController. And apply interface orientation related changes in this sub class. Extend your view controller in which you want to lock orientation with the sub class. I'll provide an example of this.

I have create class which only shows landscape orientation for view controller.

LandscapeViewController is a subclass of UIViewController in which you have to deal with orientations.

LandscapeViewController.h:

#import <UIKit/UIKit.h>

@interface LandscapeViewController : UIViewController

@end

LandscapeViewController.m:

#import "LandscapeViewController.h"

@interface LandscapeViewController ()

@end

@implementation LandscapeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        return YES;
    }
    else {
        return NO;
    }
}

@end

Extend your view controller using above subclass.

For example:

#import "LandscapeViewController.h"

@interface SampleViewController : LandscapeViewController

@end


来源:https://stackoverflow.com/questions/27009979/ios-lock-a-specific-uiviewcontroller-to-a-specific-orientation

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