首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UiNavigationController中的iOS 6 AutoRotate

UiNavigationController中的iOS 6 AutoRotate
EN

Stack Overflow用户
提问于 2013-01-27 20:04:11
回答 5查看 4.4K关注 0票数 3

我的应用程序中有UiNavigationController。我希望只有一个屏幕可以旋转,所以我放在这个类中:

代码语言:javascript
复制
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}

-(BOOL)shouldAutorotate {
    return YES;
}

-(NSInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

但发生的问题是在应用程序的屏幕上发生旋转。我怎样才能禁用它?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-01-27 22:35:18

对于iOS 6,我在我的应用程序中使用以下代码,它允许您为每个视图控制器单独指定旋转:

AppDelegate.m -

代码语言:javascript
复制
- (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;
}

ViewController.m -

代码语言:javascript
复制
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

我相信代码的原创性功劳可以参考Ray Wenderlich的"iOS 6 by Tutorials“一书。Ray Wenderlich website

票数 10
EN

Stack Overflow用户

发布于 2013-01-28 14:30:08

在您只想自动旋转的类中使用以下代码:

代码语言:javascript
复制
@interface UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
- (NSUInteger)supportedInterfaceOrientations;
- (BOOL)shouldAutorotate;
@end


@implementation UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *navController = (UINavigationController *) self.selectedViewController;
        if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
            return YES;
    }
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *navController = (UINavigationController *) self.selectedViewController;
        if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
            return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *navController = (UINavigationController *) self.selectedViewController;
        if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
            return YES;
    }
    return NO;

}

@end

在要旋转的类(即导航控制器堆栈上的前一个类)的父视图控制器中使用此代码。

代码语言:javascript
复制
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
票数 1
EN

Stack Overflow用户

发布于 2014-01-12 17:37:22

在您的appDelegate中添加以下代码

代码语言:javascript
复制
@property(nonatomic,assign)BOOL shouldRotate;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    shouldRotate=NO;
}
-(void)shouldAutoRotate:(BOOL)rotate
{
    self.shouldRotate=rotate;
}

并在rootview控制器中添加以下代码

代码语言:javascript
复制
#import "AppDelegate.h"
#define myAppDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate]

- (NSUInteger)supportedInterfaceOrientations
{
 if(![myAppDelegate shouldRotate])
     return UIInterfaceOrientationMaskPortrait;
 else
     return UIInterfaceOrientationMaskAllButUpsideDown;
}

在那之后,在你想要旋转的视图控制器中添加这个代码

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];
    [myAppDelegate shouldAutoRotate:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [myAppDelegate shouldAutoRotate:NO];
}

我为我的一个项目(IOS7)做过这样的事情,.It非常适合我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14547134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档