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

时间:2014-10-28 15:04:05

标签: ios objective-c uiviewcontroller uiinterfaceorientation

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

-(NSUInteger)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskPortrait;
}

但它仍在旋转

2 个答案:

答案 0 :(得分:9)

我建议您将应用设为纵向模式,然后在需要横向模式时再使用横向模式。

首先,如前所述点击 - >项目名称 - >一般 - >部署信息 - >仅选择“设备方向纵向”。

其次,在AppDelegate.h中添加此属性..

@property (nonatomic) BOOL fullScreenVideoIsPlaying;

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

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

执行此操作后,在您需要横向创建功能的视图控制器中,或者只是将此代码添加到viewWillAppear方法中取决于您希望如何实现此功能。

((AppDelegate *)[[UIApplication sharedApplication] delegate]).fullScreenVideoIsPlaying = YES;
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

然后,为了回到纵向模式,你可以这样做..

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

[self supportedInterfaceOrientations];

[self shouldAutorotate:UIInterfaceOrientationPortrait];

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

您可能需要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;
}

我希望它有所帮助.. :)

答案 1 :(得分:-3)

点击项目,然后从那里选择方向设置。