![](/img/trans.png)
[英]UITabBarController, UINavigationController and autorotate
[英]iOS 6 AutoRotate In UiNavigationController
我的應用程序中有UiNavigationController。 我希望只有一個屏幕可以旋轉,所以我放入這個類:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
-(BOOL)shouldAutorotate {
return YES;
}
-(NSInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
但發生的問題是在ecery屏幕上應用程序旋轉發生。 我怎么能禁用它?
對於iOS 6,我在我的應用程序中使用以下代碼,它允許您單獨指定每個viewcontroller的旋轉:
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網站
在您要僅自動旋轉的類中使用以下代碼:
@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;
}
在您的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;
}
之后在viewcontroller.m中添加此代碼,您要旋轉它
- (void)viewDidLoad
{
[super viewDidLoad];
[myAppDelegate shouldAutoRotate:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
[myAppDelegate shouldAutoRotate:NO];
}
我為我的一個項目(IOS 7)做了這個。它對我很有用。
您可以創建一個類別來覆蓋navigationController中的方法以支持所有類。
@implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end
如果要限制不同控制器中的旋轉,則覆蓋supportedInterfaceOrientations和shouldAutorote,在各個viewcontrollers中根據需要更改返回值。
-(NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.