我在尝试阻止某些视图中的方向时遇到问题,但代码不是工作属性.
我在每个视图中使用这些行:
- (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];
我希望这能帮到你们.