我在尝试阻止某些视图中的方向时遇到问题,但代码不是工作属性.
我在每个视图中使用这些行:
- (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { return UIInterfaceOrientationMaskPortrait; } else { return UIInterfaceOrientationMaskAll; } } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; }
它几乎可以在使用UINavigationController的视图中工作,但是在使用UITabBarController的视图中我遇到了很大的问题,因为它接缝代码没有被调用.
解决方法
好吧我解决了,你必须继承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];
我希望这能帮到你们.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。