shouldAutorotate、supportedInterfaceOrientations 和 preferredInterfaceOrientationForPresentation 在 iOS 7 中无法按预期工作 [英] shouldAutorotate, supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation does not work as expected in iOS 7

查看:21
本文介绍了shouldAutorotate、supportedInterfaceOrientations 和 preferredInterfaceOrientationForPresentation 在 iOS 7 中无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试阻止某些视图中的方向时遇到问题,但代码无法正常工作.

I'm having problems trying to block the orientations in some views, but the code is not working property.

我在每个视图中都使用这条线:

I'm using this lines in every view:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskPortrait;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

它几乎在使用 UINavigationController 的视图中工作,但在使用 UITabBarController 的视图中我遇到了大问题,因为它接缝表明代码没有被调用.

It almost work in the views that are using the UINavigationController, but in the ones that use the UITabBarController is where i'm having big problems, because it seams that the code is not been called.

推荐答案

好的,我解决了,你必须继承 UINavigationController 和 UITabBarController,所以代码如下:

Ok I solve it, you have to subclass UINavigationController and UITabBarController, so here is the code:

//cCustomNavigationController.h file

#import <UIKit/UIKit.h>

@interface cCustomNavigationController : UINavigationController <UINavigationControllerDelegate>

@end

//cCustomNavigationController.m file

#import "cCustomNavigationController.h"

@interface cCustomNavigationController ()

@end

@implementation cCustomNavigationController 

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

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [self.visibleViewController preferredInterfaceOrientationForPresentation];
}

@end

//cCustomTabController.h file

#import <UIKit/UIKit.h>

@interface cCustomTabController : UITabBarController <UITabBarControllerDelegate>

@end

//cCustomTabController.m file

#import "cCustomTabController.h"

@interface cCustomTabController  ()

@end

@implementation cCustomTabController

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

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}

@end

现在你只需要在你需要的地方使用这些类来创建你的 TabBarController 或 NavigationController,即

now you just have to create your TabBarController or your NavigationController using this classes where ever you need it i.e.

//For the UINavigationController
UINavigationController *navigationController = [[cCustomNavigationController alloc] init];

//For the UITabBarController
UITabBarController *tabController = [[cCustomTabController alloc] init];

希望对大家有所帮助.

这篇关于shouldAutorotate、supportedInterfaceOrientations 和 preferredInterfaceOrientationForPresentation 在 iOS 7 中无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆