当用户处于纵向视图时强制加载横向视图 [英] Forcefully load landscape view when user in Portrait view

查看:69
本文介绍了当用户处于纵向视图时强制加载横向视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用导航控制器来管理视图.我的登录页面支持纵向和横向视图.当用户登录时,我的第二个视图是 home 并且它仅支持横向模式.我想要做的是,当用户使用纵向视图登录主页时,即使设备处于纵向,主页也应显示在横向视图中.

In my Application I'm using a navigation controller to mange views. My login page support both portrait and landscape views. When user logged in my second view is home and it support only landscape mode. What I want to do is when user login to the home using portrait view home page should appear in landscape view even though the device in portrait.

所以我所做的是将状态栏的方向更改为横向主页的 viewWillAppear 方法,如下所示;

So what I did was I change the status bar orientation in to landscape int the home page's viewWillAppear method as follows;

    -(void)viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];
        [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:NO];
UIDeviceOrientation orien = [[UIDevice currentDevice] orientation];
    }

我也重写了 shouldAutorotateToInterfaceOrientation 如下

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

return YES;

}

我的问题是即使状态栏更改为横向,我的 UIViewController (home) 仍处于横向模式.当我调试时,我发现即使我将状态栏方向更改为横向,[[UIDevice currentDevice]orientation] 返回纵向.我一整天都在上网.并实施了许多其他人提供的解决方案,但我浪费了整整一天.有人可以指导我解决这些问题吗.

My problem is even the status bar changed to landscape my UIViewController (home) is remains in landscape mode. When i'm debugging I found that even I change the status bar orientation to landscape,[[UIDevice currentDevice] orientation] returns portrait. I went through internet whole day. And implement lot of solutions proviede by other but my whole day wasted. can some one guide me to solve these issue.

推荐答案

Apple 不希望您强制设备的方向.不过有一个技巧.不幸的是,我无权访问我的代码.

Apple does not want you to force the orientation of the device. There is a trick though. Unfortunately I do not have access to my code.

1. Your app in general supports all orientations. 
2. All view controllers only return their supported interface orientation in their overwrites respectivly (in supportedInterfaceOrientations). 
3. All view controllers return YES in shouldAutorotateToInterfaceOrientation only for their supported orientations. 

没关系.但它仍然需要用户实际旋转设备.否则将不会调用整个方向改变机制.现在,当您要强制更改方向时,请执行以下操作:

That is fine. But it would still require the user to actually rotate the device. Otherwise the whole orientation change mechanism would not be invoked. Now, when you want to force the orientation change, do the following:

4. Use setStatusBarOrientation to set the orientation before the next view controller is displayed. 
That alone would not do anything. Plus it would not take any effect if the next view controller is pushed. It would work fine only when the next view controller is presented modally. 
5a. So if you want to present the rotated view controller modally, then do it. 
5b. If you still need to push it then: 
5b1. Create an empty UIViewController instance. alloc/init will do. 
5b2. Present it modally
5b3. Dismiss it modally 
Now, the new view controller was not even visible to the user but the device - here comes the magic - is rotated now.  
5c4. Next push the view controller that you want to display roated. 

在你回来的路上反之亦然:)

And vice versa on your way back :)

当您使用标签栏时,上述所有内容都会变得更加复杂.你使用标签栏吗?我设法使用标签栏来实现它,我必须将其子类化以覆盖其旋转方法.在没有标签栏的应用程序中,我将 UIApplication (!) 子类化了,但不要记住这是真正需要的还是出于方便而这样做(而不是将更改应用于 50 多个视图控制器).但原则上,以上就是诀窍.

All the above gets more complicated when you use a tab bar. Do you use a tab bar? I managed to get that working with a tab bar which I had to subclass to overwrite its rotation methods. In an app without tab bar I subclassed UIApplication (!) but don't rembember wether that was really required or wether I did that out of convenience (instead of aplying the changes to 50+ view controllers). But in principle the above is it that does the trick.

PS:您可以在此处找到更详细的答案以及代码示例:呈现导航控制器横向模式不起作用 ios 6.0

PS: You find a more detailled answer here along with code samples: Presenting Navigation Controller in Landscape mode is not working ios 6.0

这篇关于当用户处于纵向视图时强制加载横向视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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