【发布时间】:2013-12-20 18:05:45
【问题描述】:
我有一个带有 2 个视图控制器的 iOS 应用程序,即 - FirstViewController 和 SecondViewController。 我的窗口的 rootViewController 是 UINavigationController。
FirstViewController 应该只能在纵向模式下工作,而 SecondViewController 只能在横向模式下工作。
搜索整个 Stackoverflow 我发现对于 iOS6 及更高版本,我必须在 UINavigationController 上创建一个类别并覆盖 -supportedInterfaceOrientations
问题
从 FirstViewController 开始。现在我的手机处于纵向模式,我按下 SecondViewController,视图以纵向模式加载。一旦我将手机旋转为横向视图,视图将旋转为横向(从此时起将不再返回纵向)。
当我弹回 FirstViewController 时,它将再次处于 Portrait 状态(无论手机的方向如何)。
我希望 SecondViewController 根本不应该以纵向模式显示。我绞尽脑汁……找不到解决办法。
APPDELEGATE
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *vc = [[ViewController alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
[self.navigationController setNavigationBarHidden:YES];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
FirstViewController
- (IBAction)btnClicked:(id)sender
{
SecondViewController *vc = [[SecondViewController alloc] init];
[self.navigationController pushViewController:vc animated:NO];
}
#pragma mark - Rotation handlers
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
SecondViewController
- (IBAction)btnClicked:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Rotation handlers
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
UINavigation 类别
@implementation UINavigationController (Rotation)
-(BOOL)shouldAutorotate
{
//return [self.topViewController shouldAutorotate];
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
@end
【问题讨论】:
-
屏蔽特定 viewController 上的方向。
标签: ios iphone objective-c