shouldAutorotate,supportedInterfaceOrientations和preferredInterfaceOrientationForPresentation在iOS 7中无法正常工作

 东营市第一中学李芳 发布于 2023-02-13 10:47

我在尝试阻止某些视图中的方向时遇到问题,但代码不是工作属性.

我在每个视图中使用这些行:

- (BOOL)shouldAutorotate
{
    return YES;
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

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

1 个回答
  • 好吧我解决了,你必须继承UINavigationController和UITabBarController,所以这里是代码:

    //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,就像你需要它一样

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

    我希望这能帮到你们.

    2023-02-13 10:50 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有