IOS7/IOS8仅在视图控制器中允许纵向

rob*_*180 2 objective-c uiviewcontroller uiinterfaceorientation ios

我正在构建一个只有1个横向视图的iPhone应用程序,所以我想阻止所有其他的景观,我试过这个:

-(NSUInteger)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

但它仍然在旋转

val*_*u17 9

我建议您只为肖像模式制作应用程序,然后在需要横向模式时再使用横向模式.

首先,如前所述,单击 - >项目名称 - >常规 - >部署信息 - >仅选择设备方向的纵向.

第二,在你AppDelegate.h添加这个属性..

@property (nonatomic) BOOL fullScreenVideoIsPlaying;
Run Code Online (Sandbox Code Playgroud)

然后,在你的AppDelegate.m我将添加此功能..

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

执行此操作后,在视图控制器中,您需要横向创建一个函数或只是将此代码添加到您的viewWillAppear方法取决于您想要如何实现这一点..

((AppDelegate *)[[UIApplication sharedApplication] delegate]).fullScreenVideoIsPlaying = YES;
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
Run Code Online (Sandbox Code Playgroud)

然后,为了设置回纵向模式,你这样做..

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.fullScreenVideoIsPlaying = NO;

[self supportedInterfaceOrientations];

[self shouldAutorotate:UIInterfaceOrientationPortrait];

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
Run Code Online (Sandbox Code Playgroud)

您可能需要iOS 8的这些功能..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

我希望它有帮助.. :)