我有一个带有 2 个 View Controller 的 iOS 应用程序,即 - FirstViewController
和 SecondViewController
。 我窗口的 rootViewController 是 UINavigationController。
FirstViewController 只能在纵向模式下工作,而 SecondViewController 只能在横向模式下工作。
搜索整个 Stackoverflow 我发现对于 iOS6 及更高版本我必须在 UINavigationController
上创建一个类别并覆盖 -supportedInterfaceOrientations
问题
从 FirstViewController 开始。现在我的手机处于纵向模式,我按下 SecondViewController, View 以纵向模式加载。一旦我将手机旋转为横屏, View 就会旋转为横屏(并且从此时起将不再返回竖屏)。
当我弹出时,FirstViewController 将再次处于纵向(无论手机的方向如何)。
我希望 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;
}
第二 View Controller
- (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
最佳答案
好吧,有点晚了,但这就是我的想法。
虽然对于 FirstVC 是人像而 Second 可以是人像和风景的情况有很多解决方案,但我找不到任何好的解决方案来解决这个问题(FirstVC ONLY 和 Second Landscape ONLY)。这是我所做的:
将两个 View Controller 嵌入到它们自己的导航 Controller 中。创建两个新类,比如 FirstNavController 和 SecondNavController 是 UINavigationController 的子类。使用这些作为您的导航 Controller 。 (如果您使用的是 StoryBoard,请选择 Navigation Controller,转到 Identity Inspector 并更改“Class”字段)。
现在,在 FirstNavController 中,添加:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
在 SecondNavController 中:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
您必须以模态方式呈现 SecondNavController。
无需在您的 View Controller 中执行任何操作。确保在应用程序设置中添加所有必需的方向。
此方法的唯一缺点是两个 View 不在同一个导航堆栈中,因为第二个 View 是模态显示的,因此您不会看到后退按钮。但是你可以自己添加一个取消/关闭按钮,并在 SecondVC 中调用 dismissViewControllerAnimated。
关于ios - 在 iOS 纵向模式下维护一个 View Controller ,在横向模式下维护另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20372223/