自动旋转锁定,以编程方式
有谁知道是否有可能以编程方式锁定 iPhone 的自动旋转,仅用于一个视图? 我想对半半透明视图提供某种帮助,但我想仅支持横向,即使所有其他视图都可以旋转。 所以我希望当该视图位于顶部时锁定旋转。
tnx
编辑:更多详细信息:一个 UIController 有 7 个 UIView...并且我希望在最后一个出现在顶部时锁定自动旋转。
Does anybody know if there is a possibility to lock autorotation of iPhone programmatically for just one view?
I want to make some kind of help with semi-transculent view, but I want to support only landscape orientation even all other views can rotate.
So I wish to lock rotation when this view is on the top.
tnx
EDIT: More details: one UIController has 7 UIView...and I wish to lock the autorotation just when the last one occurs on the top.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这个问题是一年多前提出的,但接受的方法现在已被弃用。在 iOS 6 中,所以如果有人有兴趣在 iOS 6 中执行此操作,那么您需要在最顶层的控制器上使用supportedInterfaceOrientations。
想象一下您有这样的设置...
...那么你需要在标签栏控制器上设置supportedInterfaceOrientations方法。
创建选项卡栏控制器(或导航控制器,如果位于顶部)的子类并在其中设置这些方法...
...然后在您的个人视图控制器中您可以设置您想要的属性...
This question was asked over a year ago but the accepted method is now deprecated in iOS 6 so if anybody is interested in doing this in iOS 6 then you need to use supportedInterfaceOrientations on the topmost controller.
Imagine you have a setup like this...
... then you need to set the supportedInterfaceOrientations method on the tab bar controller.
Create a subclass of the tab bar controller (or navigation controller if that is at the top) and set these methods in it...
... then in your individual view controllers you can set the properties you want...
使用以下...
Use the following...
您可以将其贴在窗户上。加载视图后,
离开此视图时将其删除。可能在
viewWillDisappear:
或viewDidDisappear:
中。You can attach it to the window. After the view is loaded, do
Remove it when leaving this view. Probably in
viewWillDisappear:
orviewDidDisappear:
.我觉得有很多具体的答案可以间歇性地工作,但是没有一个可以深入了解如果用户开始将手机倾斜到视图控制器之外,相对于应用程序的其余部分或其他视图控制器而言,后果或副作用是什么您想控制方向...
在使用它之后,您可能会意识到(像我自己一样)可能会出现不利或不期望的结果(即,当您不希望发生方向变化时发生,反之亦然)。
我的主要认识是只有“根视图控制器”才会调用“shouldAutorotate”,而不仅仅是您尝试覆盖的任何单个视图控制器。
有了这种认识,似乎很难“锁定”特定视图控制器的特定方向。
(意味着vc_A始终为纵向并且不允许更改为横向,而vc_B始终为横向并且不允许更改为纵向)
在确认这一点后,以下算法这对我有用,只能在指定的视图控制器上旋转。
设置:
首先,您必须在 info.plist 或主项目设置文件中允许您想要的方向(这些方向将是您可以在代码中使用的唯一方向)
代码:
1) 在我的根视图控制器(此处:MasterViewController)中我指定了一个 BOOL 属性(允许自动旋转)当调用“shouldAutorotate”时将使用它。
2)还使根视图控制器成为单例,以便可以从任何其他子视图控制器轻松访问它(无需传递引用)。
注意:您还可以使用观察者/通知模式或委托或其他模式,但对我来说,单例模式是最简单的
3) 添加委托“-(BOOL)shouldAutorotate”并利用 BOOL allowedAutorotate 来返回
4) 创建一个实例方法“setInterfaceOrientation”。其他一些类将在其“viewDidLoad”和/或“viewWillDisappear”中调用此方法
5) 最后在其他一些类中获取根视图控制器并相应地调用“setInterfaceOrientation”
注释:
1)此示例的结果应该是应用程序最初将以纵向加载,然后当您加载“SomeViewController”时,它将更改为横向,然后当您删除它时,它将变回纵向。
2)它的工作原理如下...
每次您物理倾斜手机时,都会调用委托“shouldAutorotate”(仅从“根视图控制器”),
并且每次您以编程方式倾斜手机时,
委托“shouldAutorotate”都会被 调用调用。
这就是为什么我们首先“allowAutorotate = YES;”,然后“倾斜手机”,然后“allowAutorotate = NO;”
因此,我们的结果是只在我们想要的时候以编程方式允许/执行方向改变一次。
GLHF!
i feel there are many specific answers that work intermittently, but none provide insight as to what the ramifications or side effects are, in respect to the rest of the app or other view controllers, if the user starts tilting the phone outside of the view controller you want to control the orientation for...
after playing around with it, you may realize (like myself) that adverse or undesired results may occur (ie. orientation changes occur when you don't want them to, or vice versa).
my main realization involved that only the 'root view controller' will invoke 'shouldAutorotate', and NOT just any individual view controller you attempt to override with.
with this realization it seemed quite difficult to 'lock' a specific orientation for a specific view controller.
(meaning have vc_A always be portrait and not allowed to change to landscape, while having vc_B always be landscape and not allowed to change to portrait)
after acknowledging this, the following algorithm is what worked for me in being able to only rotate on specified view controllers.
setup:
first you have to allow the orientations you desire, in either the info.plist or the main project settings file (these orientations will be the only ones you can use in your code)
code:
1) in my root view controller (here: MasterViewController) i designated a BOOL property (allowAutorotate) that will be utilized when 'shouldAutorotate' is invoked.
2) also make the root view controller a singleton so its easily accessible from any other child view controller (without having to pass around references).
note: you may also use the observer/notification pattern or delegation or some other pattern, but for me the singleton pattern was easiest
3) add the delegate '-(BOOL)shouldAutorotate' and utilize the BOOL allowAutorotate for its return
4) create an instance method 'setInterfaceOrientation'. some other class will call this method in their 'viewDidLoad' and/or in their 'viewWillDisappear'
5) finally in some other class get the root view controller and invoke 'setInterfaceOrientation' accordingly
notes:
1) the result of this example should be that the app will initially load in portrait, then when you load 'SomeViewController', it will change to landscape, and then when you remove it, it will change back to portrait.
2) it works like this...
every time you physically tilt the phone, the delegate 'shouldAutorotate' is invoked (only from the 'root view controller'),
as well every time you programmatically tilt the phone
the delegate 'shouldAutorotate' is invoked.
this is why we first 'allowAutorotate = YES;', then 'tilt the phone', then 'allowAutorotate = NO;'
hence, we have a result of only allowing/performing the orientation change once, programmatically, exactly when we want to.
glhf!
我找到了非常简单且有效的解决方案。通过添加顶视图,我设置了一个 BOOL,然后控制旋转。
I have found very simple and working solution. By adding the top view I set a BOOL which then controls rotating.