且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-03 09:43:01

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

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.. :)