我的应用程序中有UiNavigationController。我希望只有一个屏幕可以旋转,所以我放在这个类中:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
-(BOOL)shouldAutorotate {
return YES;
}
-(NSInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}但发生的问题是在应用程序的屏幕上发生旋转。我怎样才能禁用它?
发布于 2013-01-27 22:35:18
对于iOS 6,我在我的应用程序中使用以下代码,它允许您为每个视图控制器单独指定旋转:
AppDelegate.m -
- (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 -
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}我相信代码的原创性功劳可以参考Ray Wenderlich的"iOS 6 by Tutorials“一书。Ray Wenderlich website
发布于 2013-01-28 14:30:08
在您只想自动旋转的类中使用以下代码:
@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在要旋转的类(即导航控制器堆栈上的前一个类)的父视图控制器中使用此代码。
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}发布于 2014-01-12 17:37:22
在您的appDelegate中添加以下代码
@property(nonatomic,assign)BOOL shouldRotate;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
shouldRotate=NO;
}
-(void)shouldAutoRotate:(BOOL)rotate
{
self.shouldRotate=rotate;
}并在rootview控制器中添加以下代码
#import "AppDelegate.h"
#define myAppDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate]
- (NSUInteger)supportedInterfaceOrientations
{
if(![myAppDelegate shouldRotate])
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAllButUpsideDown;
}在那之后,在你想要旋转的视图控制器中添加这个代码
- (void)viewDidLoad
{
[super viewDidLoad];
[myAppDelegate shouldAutoRotate:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
[myAppDelegate shouldAutoRotate:NO];
}我为我的一个项目(IOS7)做过这样的事情,.It非常适合我。
https://stackoverflow.com/questions/14547134
复制相似问题