UITabBarController 与 viewControllers 利用不同的方向?
我可以看到这一直困扰着很多人:/
我有一个 UITabBarController,它有 4 个 viewController,全部都是 UINavigationController 类型。 导航控制器之一将视图控制器推送到其堆栈上,该视图控制器应以横向模式/方向呈现。
viewController 是一个图表,它是应用程序中景观唯一有意义的地方。 (当出现 UITabBar 时,我隐藏 UITabBar,以免让用户相信这在任何地方都有效)
为了使 UITabBarController 正确响应方向的变化,所有它的 viewController 都需要从委托方法返回相同的值:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
所以要适应这种行为我已经在属于 UITabBarController 的所有 viewController 中实现了这个方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL canRotate = [defaults boolForKey:@"can_rotate"];
return canRotate;
}
现在的“技巧”是,当我的 can-be-landscape viewController 被推入时,我会这样做:
- (void) viewWillAppear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"can_rotate"];
[defaults synchronize];
}
当它被弹出时,我会这样做:
- (void) viewWillDisappear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:NO forKey:@"can_rotate"];
[defaults synchronize];
}
这非常有效。当 viewController 位于堆栈上时,我可以旋转设备,视图也会随之旋转。
然而问题是,如果用户在横向模式下点击导航栏上的“后退”按钮,从而将 viewController 弹出到前一个 viewController,那么这个“旧”viewController 当然也处于横向模式。更糟糕的是,因为我将 BOOL 设置为 NO,所以当我将设备定向为纵向模式时,这个“旧”viewController 无法旋转回来。
有没有办法更新所有内容,以便当我弹出可以处于横向模式的视图控制器时,我的其他视图控制器都不会处于横向模式?
我有点担心这是否可以从横向到纵向完成,从纵向到横向也应该是可能的,从而使我的“黑客”不必要..但如果不能,那么我回到第一个:/
希望我很接近并且有人可以帮助我在那里,谢谢:)
I can see that this is something that has been troubling a lot of people:/
I have a UITabBarController that has 4 viewControllers, all of type UINavigationController.
One of the navigationControllers gets a viewController pushed onto its stack, this viewController should be presented in landscape mode/orientation.
The viewController is a graph, it is the absolutely only place in the app where landscape makes sense. (I hide the UITabBar when this is presented to not lead the user to believe this will work everywhere)
To make a UITabBarController respond correctly to changes in orientation all its viewControllers need to return the same value from the delegate method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
So to accomodate this behavior I have implemented this method in all the viewControllers belonging to the UITabBarController:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL canRotate = [defaults boolForKey:@"can_rotate"];
return canRotate;
}
The "trick" is now that when my can-be-landscape viewController is pushed I do this:
- (void) viewWillAppear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"can_rotate"];
[defaults synchronize];
}
and when it is popped, I do this:
- (void) viewWillDisappear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:NO forKey:@"can_rotate"];
[defaults synchronize];
}
This works really well. When the viewController is on the stack I can rotate the device and the view follows.
The problem is however, that if the user taps the "back" button on the navigationBar while in landscape mode, thus popping the viewController to the previous viewController, this "old" viewController is of course also in landscape mode. To make things worse, because I set the BOOL to NO, this "old" viewController can not rotate back when I orientate the device to portrait mode.
Is there a way to update everything so that none of my other viewControllers will be in landscape mode when I pop the can-be-in-landscape mode viewController?
I am a bit worried that if this could be done from landscape to portrait it should also be possible from portrait to landscape, thus making my "hack" unnecessary.. but if it can not, then I am back to square one :/
Hope I am close and that someone could help me get there, thanks:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我可以简化整个事情......
<代码>
I think I can simplify the whole thing....