我有一个带有2个视图控制器的iOS应用程序,即 FirstViewController
和 SecondViewController
. My window's rootViewController is UINavigationController .
FirstViewController应仅在纵向模式下工作,而SecondViewController仅在横向模式下工作 .
搜索遍布Stackoverflow我发现对于iOS6及更高版本我必须在 UINavigationController
上创建一个类别并覆盖 -supportedInterfaceOrientations
THE PROBLEM
从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 Category
@implementation UINavigationController (Rotation)
-(BOOL)shouldAutorotate
{
//return [self.topViewController shouldAutorotate];
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
@end
2 回答
嗯,这有点晚了,但这就是我的想法 .
虽然有很多解决方案适用于FirstVC是Portrait和Second可以是Portrait和Landscape的情况,但我找不到任何解决这个问题的好方法(仅限第一张肖像和仅第二张风景) . 这是我做的:
将两个视图控制器嵌入到自己的导航控制器中 . 创建两个新类,比如说FirstNavController和SecondNavController子类化UINavigationController . 使用这些作为您的导航控制器 . (如果您使用的是StoryBoards,请选择导航控制器,转到Identity Inspector并更改“Class”字段) .
现在,在FirstNavController中,添加:
在SecondNavController中:
您必须以模态方式呈现SecondNavController .
您的视图控制器无需任何操作 . 确保在应用程序设置中添加所有必需的方向 .
这种方法的唯一缺点是两个视图不在同一个导航堆栈中,因为第二个视图是以模态方式呈现的,因此您将看不到后退按钮 . 但是你可以自己添加一个取消/关闭按钮,并在SecondVC中调用dismissViewControllerAnimated .
在我以前的应用程序中,我已经完成了
首先,您需要启用纵向,横向左侧,横向右侧方向投影
Now
将下面的代码设置为FirstViewController
将下面的代码设置为您的secondViewController