Xcode - 使用 XIB 以纵向锁定除一个视图之外的所有视图 [英] Xcode - Lock all views in portrait except one view using XIB

查看:31
本文介绍了Xcode - 使用 XIB 以纵向锁定除一个视图之外的所有视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助.

我目前正在 xcode 中做一个应用程序,我需要将所有视图锁定为纵向模式,但我有第二个视图控制器需要进入横向模式,因为我正在使用媒体播放器框架来显示视频和需要在横向.

I'm currently doing an application in xcode where i need to have all views locked in portrait mode, but i have a second view controller that needs to go in landscape because i'm using mediaplayer framework to show a video and that needs to be in landscape.

我一直在寻找这个,大约一个星期了,但仍然无法解决问题.

I have been searching for this, for around a week now and still can't solve the problem.

非常感谢您的帮助.

推荐答案

这有两个方面.1. 如何根据需要锁定每个视图控制器的方向.

This has two aspects. 1. How to lock the orientation for each view controller as requierd.

但仅此而已并不能旋转设备.当您处于纵向并且下一个要显示的视图控制器是横向时,它仍然会以纵向显示.您可以将设备转动到 portait,控制器会相应地旋转.一旦进入横向,它就固定在横向中,不能再旋转了.当你回到你的肖像控制器时,它会以横向呈现......

But that alone does not rotate the device. When you are in portrait and the next view controller to be shown is for landscape, then it would still be presented in portrait. You could turn the device to portait and the conroller would rotate accordingly. Once in landscape it is fixed in landscape and cannot be rotated any more. When you go back then to your portrait controller, it would be presented in landscape ...

因此,2. 您需要旋转设备.

So, 2. you need to rotate the device.

第一桥很容易.

在所有修复了纵向的控制器中实现这一点:

Implement this in all controllers that are fix for portrait:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

并为景观控制器实现这一点:

And implement this for the landscape controllers:

- (BOOL)shouldAutorotate
{
    return YES;
}


- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

2.对于下一步,您需要应用一个技巧.您不能以编程方式转动设备.实现这一点的唯一方法是旋转状态栏.之后,下一个模态 (!) 视图控制器以新的方向呈现.这不适用于在导航堆栈上推送控制器.诀窍是以模态呈现任何(空)视图控制器并立即将其删除.现在设备已转动,您可以将视图控制器推送到堆栈.这是我前几天使用的代码:

2. For the next step you need to apply a trick. You cannot programatically turn the device. The only way achieving that is rotating the status bar. After that the next modal (!) view controller is presented in the new orientation. This does not work for pushing controllers on the navigation stack. The trick is to present any (empty) view controller modally and remove it straight away. Now the device is turned and you can push a view controller to the stack. This is the code, that I used the other day:

// Fetch the status bar from the app and set its orientation as required. 
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

// Create an empty view controller, present it modally and remove it directly afterwards
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
// Now the device is rotated to the desired orientation. Go from there.

如果您无论如何都在使用模态视图控制器,那么它当然会更简单一些.

If you are working with modal view controllers anyway, then it is a bit simpler of course.

这篇关于Xcode - 使用 XIB 以纵向锁定除一个视图之外的所有视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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