iOS7/IOS8在视图控制器中仅允许纵向 [英] IOS7/IOS8 Allow only portrait in view controller

查看:45
本文介绍了iOS7/IOS8在视图控制器中仅允许纵向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building an app for iPhone that will have only 1 landscape view, and so i want to block landscape for all other, i have tried this:

-(NSUInteger)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskPortrait;
}

但是它仍然旋转

推荐答案

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

I will suggest to just make your app for portrait mode and then whenever you need the landscape mode then allow landscape mode.

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

First, as previously suggested click on -> Project name -> General -> Deployment Info -> Only select Portrait for Device Orientation.

第二,在您的 AppDelegate.h 中添加此属性.

Second, in your AppDelegate.h add this property..

@property (nonatomic) BOOL fullScreenVideoIsPlaying;

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

Then, on your AppDelegate.m I will add this function..

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

完成此操作后,在您需要使用景观的视图控制器中创建一个函数或仅将此代码添加到您的 viewWillAppear 方法中即可.

After doing this, in the view controller that you need landscape create a function or just add this code to your viewWillAppear method is depending how you want to accomplish this..

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

然后将其设置回纵向模式.

Then for setting back to portrait mode you do this..

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

[self supportedInterfaceOrientations];

[self shouldAutorotate:UIInterfaceOrientationPortrait];

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

iOS 8可能需要这些功能.

You might need these functions for 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;
}

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

I hope it helps.. :)

这篇关于iOS7/IOS8在视图控制器中仅允许纵向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆