xcode iOS 6 - (BOOL)shouldAutorotate 不会因为导航控制器被推送视图控制器而被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12827804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 6 - (BOOL)shouldAutorotate not getting called for navigation controllers pushed viewControllers
提问by Aditya Deshmane
For my app rootViewController
is navgationController
.
对于我的应用程序rootViewController
是navgationController
.
I found that pushed controller's
我发现推控制器的
-(BOOL)shouldAutorotate
is not getting called.
-(BOOL)shouldAutorotate
没有被调用。
and
和
-(NSUInteger)supportedInterfaceOrientations
get called only once.
-(NSUInteger)supportedInterfaceOrientations
只被调用一次。
I have checked correctly in xcode's
project summary (or plist
) for windows all orientation support.
我已在xcode's
项目摘要(或plist
)中正确检查了 windows 所有方向支持。
I want these method to get called, as there is some uicontrol positioning code which i want to execute programmatically for orientation change.
我希望调用这些方法,因为有一些 uicontrol 定位代码我想以编程方式执行以更改方向。
I solved this problem by overriding (category) navigation controller's following methods
我通过覆盖(类别)导航控制器的以下方法解决了这个问题
-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;
I checked which controller is getting pushed and accordingly called respective pushed controller's uicontrol positioning code in Navigation controller's following method
我检查了哪个控制器被推送,并相应地在导航控制器的以下方法中调用了相应的推送控制器的 uicontrol 定位代码
(NSUInteger)supportedInterfaceOrientations;
This is working fine but i dont think this is correct way. Please help me out for better solution.
这工作正常,但我认为这不是正确的方法。请帮助我寻求更好的解决方案。
回答by Shreesh Garg
You can check the following link, you need to create custom navigation to support should auto rotate
您可以查看以下链接,您需要创建自定义导航以支持应自动旋转
http://mobileappdevpage.blogspot.in/2012/11/how-to-use-should-autorotateios-6-with.html
http://mobileappdevpage.blogspot.in/2012/11/how-to-use-should-autorotateios-6-with.html
The other way you can do this by creating category of UINaviagationController
另一种方法是创建 UINaviagationController 类别
code for .h file is
.h 文件的代码是
@interface UINavigationController (autorotation)
-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;
and code for .m file is
.m 文件的代码是
@implementation UINavigationController (autorotation)
-(BOOL)shouldAutorotate
{
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
[self.topViewController shouldAutorotate];
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
@end
回答by anoop4real
I also faced the same issue with the navigation controller. It works fine in the case of all the pushed view controllers, but my scenario was quite different I had one viewcontroller pushed in to the navigation controller (ViewControllerParent) as the root,
我也遇到了与导航控制器相同的问题。在所有推送的视图控制器的情况下它都可以正常工作,但我的场景完全不同,我将一个视图控制器作为根推送到导航控制器(ViewControllerParent),
NavController
-- rootViewController (ViewControllerParent)
--- ViewControllerChild1
--- ViewControllerChild2
Due to some project requirement, I was keeping the ViewControllerParent as base, and then I was adding Child viewcontroller's view as subviews to the parent based on user actions. Now I had a scenario where I wanted Child1 not to rotate and Child2 to have rotation.
由于某些项目要求,我将 ViewControllerParent 作为基础,然后根据用户操作将子视图控制器的视图作为子视图添加到父视图。现在我有一个场景,我希望 Child1 不旋转而 Child2 旋转。
The problem I faced was that my [self.topViewController] in the Navigation controller class always returns me the Parent Object since I am not pushing the Childs into the nav stack. So my shouldAutorotate methods in my Childs will never be called. So I had to put a class check in the shouldAutorotate method of my parent and then return the rotation value. I did something like this in parent class (ViewControllerParent),it is a kind of workaround but it fixed the issue
我面临的问题是我在导航控制器类中的 [self.topViewController] 总是返回父对象,因为我没有将 Childs 推入导航堆栈。所以我的 Childs 中的 shouldAutorotate 方法将永远不会被调用。所以我不得不在我父母的 shouldAutorotate 方法中进行类检查,然后返回旋转值。我在父类(ViewControllerParent)中做了类似的事情,这是一种解决方法,但它解决了问题
-(BOOL)shouldAutorotate
{
BOOL allowRotation = YES;
if ([currentlyLoadedChild isKindOfClass:[Child1 class]])
{
allowRotation = NO;
}
if ([currentlyLoadedChild isKindOfClass:[Child2 class]])
{
allowRotation = YES;
}
return allowRotation;
}
-anoop
-anoop
回答by Gishantha Jayasooriya
I had the same issue. check this answerits a great approauch rather than applying ShouldAutoRotate
我遇到过同样的问题。检查这个答案它是一个很好的方法,而不是应用 ShouldAutoRotate
回答by Mundi
You can check for the interface orientation via
您可以通过以下方式检查界面方向
[UIApplication sharedApplication].statusBarOrientation
when the view controller is loaded, say, in viewWillAppear
. There you can do your layout of subviews. Once the view is up, shouldAutorotate
will be called whenever the device is turned.
当视图控制器加载时,比如说,在viewWillAppear
. 在那里你可以做你的子视图布局。视图启动后,shouldAutorotate
每当设备转动时都会调用。
回答by shabbirv
Overriding UINavigationController is the right approach, but I'm not sure if you're checking the push controllers supportedInterfaceOrientations the correct way.
覆盖 UINavigationController 是正确的方法,但我不确定您是否以正确的方式检查推送控制器支持的接口方向。
Look at my answer here: https://stackoverflow.com/a/12669343/253008