UINavigationController中的ViewController方向更改 [英] ViewController in UINavigationController orientation change
问题描述
所以我有以下层次结构:
So I have the following hierarchy:
UINavigationController
- > RootViewController( UIViewController
) - > UITableViewController
- > DetailViewController( UIViewController
)
UINavigationController
--> RootViewController (UIViewController
) --> UITableViewController
--> DetailViewController (UIViewController
)
我想将RootViewController的方向锁定为仅限肖像,但保留其余视图控制器的所有方向。
I want to lock the orientation on RootViewController to Portrait only, but leave all orientations for the rest view controllers.
如果我把它放到子类 UINavigationController
:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
然后所有视图控制器都锁定为肖像。
All view controllers are then locked to portrait.
我的问题是,有没有办法只将RootViewController锁定为Portrait,但保留其他视图控制器的所有选项?
My question is, is there a way to lock only RootViewController to Portrait, but leave all options for other view controllers?
推荐答案
检查此处的链接,以便在iOS 6中修复自动旋转并根据每个视图设置方向支持: http://www.disalvotech.com/blog/app-development/iphone/ios-6-rotation-solution/
check the link here for fixing autorotation in iOS 6 and set orientation support per view basis: http://www.disalvotech.com/blog/app-development/iphone/ios-6-rotation-solution/
以下是您可以做的事情:
Here is what you could do:
-
创建一个自定义导航控制器,它是
.m文件中的> UINavigationController
:
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
在你的 AppDelegate.h
,
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
UINavigationController *navigationController;
ViewController *viewController;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
和 AppDelegate.m
,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// set initial view
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
viewController = [[ViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
navigationController = [[CustomNavigationController alloc]
initWithRootViewController:viewController]; // iOS 6 autorotation fix
[navigationController setNavigationBarHidden:YES animated:NO];
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController]; // iOS 6 autorotation fix
//[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window // iOS 6 autorotation fix
{
return UIInterfaceOrientationMaskAll;
}
在你的rootViewController中,无论事件是什么推动执行此操作:
in your rootViewController, for whatever the event push the second view controller, do this:
- (IBAction)pushSecondViewController:(id)sender {
// push second view controller
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
}
,添加
in your each view controller, add
- (BOOL)shouldAutorotate{}
- (NSUInteger)supportedInterfaceOrientations{}
对于iOS 6,您可以设置每个视图控制器,无论您需要单独的方向支持。
for iOS 6, you can set each view controller whatever the orientation support you want individually.
对于iOS 5及以下版本,您可以设置
for iOS 5 and below, you can set
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{}
所有学分都归John DiSalvo所有,他在链接中编写了示例应用程序。
All the credits goes to John DiSalvo who wrote the sample app in the link.
希望这会有所帮助。
这篇关于UINavigationController中的ViewController方向更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!