Iphone iOS6:supportedInterfaceOrientations不工作(已调用,但接口仍在旋转)
在我的应用程序中,我有多个视图,一些视图需要同时支持纵向和横向,而其他视图只需要支持纵向。因此,在项目摘要中,我选择了所有方向 以下代码用于在iOS 6之前的给定视图控制器上禁用横向模式:Iphone iOS6:supportedInterfaceOrientations不工作(已调用,但接口仍在旋转),iphone,objective-c,ios,ios6,Iphone,Objective C,Ios,Ios6,在我的应用程序中,我有多个视图,一些视图需要同时支持纵向和横向,而其他视图只需要支持纵向。因此,在项目摘要中,我选择了所有方向 以下代码用于在iOS 6之前的给定视图控制器上禁用横向模式: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (inte
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
由于iOS6中不推荐使用shouldAutorotateToInterfaceOrientation,我已将上述内容替换为:
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMask.Portrait;
}
当视图出现时,此方法被正确调用(我可以设置断点以确保这一点),但界面仍会旋转到横向模式,而不管我仅返回纵向模式的遮罩。我做错了什么
目前似乎不可能构建每个视图具有不同方向要求的应用程序。它似乎只遵循项目摘要中指定的方向。首先尝试添加
shouldAutorotate
method为了使应用程序在唯一模式下工作,您应该返回UIInterfaceOrientationMaskLandscape
。如果您只想保持纵向模式,那么您的操作是正确的
只需在Info.plist中添加UISupportedInterfaceOrientations
键,并指定应用程序打算保留的界面方向值
此外,如果您想完全避免自动旋转,您应该从
shouldAutoRotate
返回false。但是我建议您从这里返回true,并在支持的InterfaceOrientations
方法中指定正确的方向。尝试在AppDelegate.m中更改此代码
// self.window.rootViewController = self.navigationController;
[window setRootViewController:navigationController];
这是完整的答案
XD在我的例子中,我有UINavigationController和视图控制器。我必须将UINavigationController子类化,为了只支持纵向,我添加了以下方法:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
因此,在UINavigationController子类中,我需要检查当前topViewController支持哪个方向
- (NSUInteger)supportedInterfaceOrientations
{
return [[self topViewController] supportedInterfaceOrientations];
}
我发现的一件事是,如果您有一个仍在运行的旧应用程序
[window addSubView:viewcontroller.view]; //This is bad in so may ways but I see it all the time...
您需要将其更新为:
[window setRootViewController:viewcontroller]; //since iOS 4
执行此操作后,方向应再次开始工作。如果您使用UINavigationController作为根窗口控制器,则它将其
应自动旋转
&支持的接口方向将被调用
确定是否正在使用UITabBarController,依此类推
因此,要做的事情是对导航/选项卡栏控制器进行子类化,并覆盖其shouldAutorotate
和支持的interfaceOrientations
方法。此代码适用于我:
-(BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
看看我自己的答案我的情况和你一样。我知道你已经接受了一个答案,但我想还是再加一个吧。这就是我理解新版轮换系统工作的方式。根视图控制器是唯一被调用的视图控制器。我认为,原因是,对于子视图控制器,旋转它们的视图是没有意义的,因为它们无论如何都会停留在根视图控制器的框架内
那么,会发生什么呢。首先在根视图控制器上调用shouldAutorotate
。如果返回NO
,则所有操作都停止。如果返回YES
,则调用supportedInterfaceOrientations
方法。如果在此方法中确认了接口方向以及Info.plist或应用程序委托支持的全局方向,则视图将旋转。在旋转之前,应自动查询正向旋转方法
方法。如果YES
(默认设置),则所有子项都将收到will
和didRotateTo…
方法以及父项(然后他们将其转发给其子项)
我的解决方案(直到有更有力的解决方案)是在supportedInterfaceOrientions
方法期间查询最后一个子视图控制器并返回其值。这使我可以旋转一些区域,同时只保留其他区域的纵向。我意识到它很脆弱,但我看不到另一种方法不涉及事件调用、回调等的复杂化。如果您使用UINavigationController
,您必须在UINavigationController
的子类中实现shouldAutorotate
和支持的接口方向
这些可以通过两个步骤进行控制,如果shouldAutorotate
返回YES,则有效支持的接口方向
。这是一个很好的组合
在本例中,除CoverFlowView和PreviewView外,我的大多数视图都是纵向视图。
CoverFlowView转换为PreviewView,PreviewView希望跟随CoverFlowView的旋转
@implementation MyNavigationController
-(BOOL)shouldAutorotate
{
if ([[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"PreviewView")])
return NO;
else
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
if ([[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"CoverFlowView")])
return UIInterfaceOrientationMaskAllButUpsideDown;
else
return UIInterfaceOrientationMaskPortrait;
}
...
@end
我的解决方案:子类化UINavigationController
,并将其设置为window.rootViewController
层次结构的top viewcontroller将控制方向,一些代码示例:我认为最好的方法是进行分类,而不是将UINavigationController
或UITabbarController
您的UINavigationController+旋转.h
#import <UIKit/UIKit.h>
@interface UINavigationController (Rotation)
@end
试着让你所有的控制器导入这个类别,这就像一个魅力。
您甚至可以使一个控制器不旋转并推送另一个将旋转的控制器。Ray Wenderlich团队的“iOS6 By Tutorials”中特别提到了iOS6的最佳方法,并且在大多数情况下比将UINavigationController子类化要好
我将iOS6与一个故事板一起使用,其中包含一个UINavigationController集作为初始视图控制器
- (NSUInteger)supportedInterfaceOrientations
{
return [[self topViewController] supportedInterfaceOrientations];
}
//AppDelegate.m-此方法在iOS6之前不可用
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}
//m-返回您选择的任何方向
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
- (UIViewController*)terminalViewController:(UIViewController*)viewController
{
if ([viewController isKindOfClass:[UITabBarController class]])
{
viewController = [(UITabBarController*)viewController selectedViewController];
viewController = [self terminalViewController:viewController];
}
else if ([viewController isKindOfClass:[UINavigationController class]])
{
viewController = [[(UINavigationController*)viewController viewControllers] lastObject];
}
return viewController;
}
- (NSUInteger)application:(UIApplication *)application
supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
UIViewController* viewController = [self terminalViewController:window.rootViewController];
if (viewController)
orientations = [viewController supportedInterfaceOrientations];
return orientations;
}
(BOOL)shouldAutorotate
{
return NO;
}
(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
@implementation CustomNavigationController
// -------------------------------------------------------------------------------
// supportedInterfaceOrientations:
// Overridden to return the supportedInterfaceOrientations of the view controller
// at the top of the navigation stack.
// By default, UIViewController (and thus, UINavigationController) always returns
// UIInterfaceOrientationMaskAllButUpsideDown when the app is run on an iPhone.
// -------------------------------------------------------------------------------
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
// -------------------------------------------------------------------------------
// shouldAutorotate
// Overridden to return the shouldAutorotate value of the view controller
// at the top of the navigation stack.
// By default, UIViewController (and thus, UINavigationController) always returns
// YES when the app is run on an iPhone.
// -------------------------------------------------------------------------------
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}