繁体   English   中英

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

[英]IOS7/IOS8 Allow only portrait in view controller

我正在为iPhone开发一个应用程序,该应用程序仅具有1个景观视图,因此我想为所有其他视图阻止景观,我已经尝试过:

-(NSUInteger)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskPortrait;
}

但它仍然旋转

我建议您将应用程序设置为纵向模式,然后在需要横向模式时再允许横向模式。

首先,按照先前的建议,单击->项目名称->常规->部署信息->仅选择纵向作为设备方向。

其次,在您的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;
}

希望对您有所帮助。:)

单击项目,然后从中选择方向设置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM