如何锁定iOS 8中Objective-C iPhone应用程序的特定视图的方向?

rob*_*ong 5 objective-c orientation ios

我只是想设置我的应用程序,以便只能在横向模式下查看一个视图.

我已经尝试过几乎一切从shouldAutorotatesupportedInterfaceOrientationpreferedInterfaceOrientationForPresentation,并且设置[UIDevice currentDevice]的方向为纵向.我在Info.plist和项目的常规部分启用了Landscape.

Gur*_*ngh 14

首先只在info.plist中创建应用程序纵向

在AppDelegate类中创建以下属性:

@property BOOL restrictRotation;
Run Code Online (Sandbox Code Playgroud)

然后在AppDelegate.m类中创建以下方法:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if(self.restrictRotation)
        return UIInterfaceOrientationMaskPortrait;
    else
        return UIInterfaceOrientationMaskAll;
}
Run Code Online (Sandbox Code Playgroud)

在ViewController中创建以下方法,并在要允许横向方向之前调用它.(在viewDidLoad方法中首先调用true,以确保限制旋转)

-(void) restrictRotation:(BOOL) restriction
{
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = restriction;
}
Run Code Online (Sandbox Code Playgroud)

像这样:

[self restrictRotation:NO];
Run Code Online (Sandbox Code Playgroud)

在完成景观视图并将其解散后,请立即致电:

[self restrictRotation:YES];
Run Code Online (Sandbox Code Playgroud)

希望这能回答你的问题.