0

在我的 iphone 应用程序中,我已将支持的方向标记为如下屏幕截图。

在此处输入图像描述

但我想阻止一些关于自动旋转的看法,因为我使用下面的代码(ios6)

编辑

-(BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}


- (BOOL)shouldAutorotate
{
return NO;
 }

但视图仍然旋转,请帮我解决问题?

4

6 回答 6

4

iOS 6.0 引入了新的旋转方法并且不调用shouldAutorotateToInterfaceOrientation:.

您需要实现这些方法以同时支持 pre-iOS 6.0 和 iOS 6.0+:

// Do as many checks as you want to allow for other orientations here for pre-iOS 6

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

// You can return YES here as this still checks supportedInterfaceOrientations
// before rotating, or you can return NO to 'lock' the view in whatever it's in... 
// Making sure to return the appropriate value within supportedInterfaceOrientations

- (BOOL)shouldAutorotate
{
    return YES;
}

// return supported orientations, bitwised OR-ed together

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

编辑-如果您UIViewController在 aUINavigationController的层次结构中

如果您的视图控制器在 aUINavigationController的视图层次结构中,则实际上在导航控制器(而不是视图控制器)上调用了旋转方法……在我看来,Apple 实现了这一点很有趣,但是您可以采取以下措施来允许视图控制器来响应这些方法,而不是-

使用以下方法创建一个类别UINavigationController

// UINavigationController+Additions.h file

@interface UINavigationController (Additions)
@end




// UINavigationController+Additions.m file

#import "UINavigationController+Additions.h"

@implementation UINavigationController (Additions)

- (BOOL)shouldAutorotate
{
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    UIViewController *viewController = [self topViewController];

    if ([viewController respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)])
    {
        return [viewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    }

    return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

- (NSUInteger)supportedInterfaceOrientations
{
    UIViewController *viewController = [self topViewController];

    if ([viewController respondsToSelector:@selector(supportedInterfaceOrientations)])
    {
        return [viewController supportedInterfaceOrientations];
    }

    return [super supportedInterfaceOrientations];
}

@end

编辑 2 - 如果您UIViewController在 aUITabBarController的标签内

如果您的视图控制器位于 aUITabBarController的选项卡中,也会出现同样的问题。

再次创建一个类别UITabBarController以允许视图控制器响应:

// UITabBarController+Additions.h file

@interface UITabBarController (Additions)
@end

// UITabBarController+Additions.m file

#import "UITabBarController+Additions.h"

@implementation UITabBarController (Additions)

- (BOOL)shouldAutorotate
{
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    UIViewController *selectedViewController = [self selectedViewController];

    if ([selectedViewController respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)])
    {
    return [selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    }

    return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

- (NSUInteger)supportedInterfaceOrientations
{
    UIViewController *selectedViewController = [self selectedViewController];

    if ([selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
    {
        return [selectedViewController supportedInterfaceOrientations];
    }

    return [super supportedInterfaceOrientations];
}

@end

编辑 3

这里还有一些关于 Objective-C 类别的链接:

http://macdevelopertips.com/objective-c/objective-c-categories.html

http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/

于 2013-02-26T05:11:08.997 回答
0

shouldAutorotateToInterfaceOrientation在 iOS 6 中已弃用。

查看此问题的答案以获取更多详细信息。

于 2013-02-26T05:11:02.117 回答
0

如果你想修复特定视图的方向试试这个,

-(BOOL)shouldAutorotate
{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait; //IOS_6
}

编辑

当你有标签栏时,你需要解决这个问题。请看链接。希望能有所帮助。

  1. 链接 1
  2. 链接 2
于 2013-02-26T05:15:09.557 回答
0

据我所知,你最好也写另外两个方法如下

- (BOOL)shouldAutorotate
{
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
于 2013-02-26T05:16:00.000 回答
0

你真的想要你的方向颠倒还是只是纵向?我为纵向构建了我的应用程序,请尝试此代码(对于纵向,如果其他,请修改)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if(interfaceOrientation == UIInterfaceOrientationPortrait)
    return YES;

return NO;
}
于 2013-02-26T05:16:46.107 回答
0

当涉及UINavigationControlleror时,将/和UITabBarController子类化。 UINavigationControllerUITabBarControlleroverriding supportedInterfaceOrientations

 #import "UINavigationController+Orientation.h"

 @implementation UINavigationController (Orientation)

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

@end  

// 为了UITabBarController

#import "UITabBarController+Orientation.h"

@implementation UITabBarController (Orientation)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController  shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return YES;
}

@end

现在,iOS 容器(例如 UINavigationController)不会咨询它们的子容器来确定它们是否应该自动旋转。
如何子类
1. 添加一个新文件(cocoa touch 下的Objective c- category)
2. Category: Orientation Category On: UINavigationController
3. 将以上代码添加到UINavigationController+Orientation.m

于 2013-02-26T05:33:27.210 回答