tabBarController和横向模式,插曲II navigationControllers
-
09-09-2019 - |
题
我有一个的UITabBarController,每个选项卡处理不同的UIViewController,由于需要在层叠新的控制器推。在两个这样的标签,我需要的,在达到特定的控制器时,旋转iPhone和可视化横向模式视图的能力。挣扎了很多之后,我发现,它是强制性的子类的UITabBarController覆盖shouldAutorotateToInterfaceOrientation。然而,如果我只是在执行返回YES,下面的不希望的副作用出现了:
旋转iPhone当在每个标签的每个控制器被自动置于横向模式。
在每个控制器返回NO不起作用即使上位shouldAutorotateToInterfaceOrientation:当iPhone旋转时,所述控制器被放置在横向模式
我实现shouldAutorotateToInterfaceOrientation作为子类的UITabBarController如下:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if([self selectedIndex] == 0 || [self selectedIndex] == 3)
return YES;
return NO;
}
因此,只有两个选项卡我感兴趣实际上得到了横向模式的支持。 有支持风景模式用于特定选项卡的堆栈上的特定的控制器的方法吗?
我尝试,但没有成功,像
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if([self selectedIndex] == 0 || [self selectedIndex] == 3)
{
if ([[self selectedViewController] isKindOfClass: [landscapeModeViewController class]])
return YES;
}
return NO;
}
此外,我试图使用委托方法didSelectViewController,没有成功。 任何帮助是极大的赞赏。 谢谢你。
解决方案
这为我工作:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(self.selectedIndex == 0 && [[[self.viewControllers objectAtIndex:0] visibleViewController] isKindOfClass:[MyViewController class]])
return YES;
else
return NO;
}
其他提示
下面是一个扩展的UITabBarController各位代表呼吁shouldAutorotateToInterfaceOrientation
到当前选定的子控制器。使用这个扩展,你并不需要继承的UITabBarController了,你可以在你的控制器使用shouldAutorotateToInterfaceOrientation
像预期。
的UITabBarController + Autorotate.h:
#import <UIKit/UIKit.h>
@interface UITabBarController (Autorotate)
@end
的UITabBarController + Autorotate.m:
#import "UITabBarController+Autorotate.h"
@implementation UITabBarController (Autorotate)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
UIViewController *controller = self.selectedViewController;
if ([controller isKindOfClass:[UINavigationController class]])
controller = [(UINavigationController *)controller visibleViewController];
return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
@end
我已经能够没有问题到现在(从我的应用程序的标签栏控制器)使用了一段时间:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
这样的话,在适当的VC,我们得到做的真正的检查,在这种情况下,要相片库视图(什么?):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
我的相册查看是不是即使在对于给定的导航控制器顶的堆栈。它仍然被调用。
唉,我才发现,原来这的不的工作这么好当VC是在 MoreViewController (而不是四个主选项卡)潜伏。在这种情况下,我的画廊VC不会被调用。我想这是因为我一直都沿着调用VC是真正从所选选项卡导航控制器,其中的然后的东西传播到相应的VC,在这种情况下,我的照片库VC。但对于更多的VC,事情不工作得这么好...... aaaand事情旋转走下坡路了。 :\
我试图由Andreas使用修饰(见在此线程其他地方),但没有成功。线索欢迎!
我碰到了同样的问题你没有用的UITabBarController工作时。我需要控制哪些UIViewControllers被允许旋转,哪些不是。我的主要问题是与更多的标签。我不想让任何包含在更多选项卡中的UIViewControllers的旋转。
我的解决办法是创建自己的UITabBarController我把它叫做MyTabBarController:
@interface MyTabBarController : UITabBarController <UITabBarDelegate> {
}
然后我实现了shouldAutorotateToInterfaceOrientation方法:
@implementation MyTabBarController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
UIViewController *controller = [self selectedViewController];
if ((controller == [self moreNavigationController]) || ([self selectedIndex] == 4))
{
return interfaceOrientation == UIInterfaceOrientationPortrait;
}
return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
@end
我需要如果选择更多标签来发现。这是一个两个步骤的过程;当最初被选择更多标签的API返回一个的selectedIndex高于4所以我需要与moreNavigationController选定控制器比较。
如果一个UIViewController中从更多选项卡内选择然后将selectedIndex是最后4但selectedController不是moreNavigationController了,但该选择的UIViewController
在如果((控制器== [自moreNavigationController])||([自的selectedIndex] == 4))强>取这个问题的关心。
现在,当我运行我的应用我在更多选项卡UIViewControllers不旋转。我希望这将有助于谁正在运行到同样的问题,我还做了其他的开发者。
埃米利奥
从我在这里看到的和其他地方我已缝合在一起使用该方法shouldAutorotate
因为老shouldAutorotateToInterfaceOrientation
已弃用的溶液中。
我已经把它的类别的UITabBarController内部。 我所以希望这是容许的!
// call to method shouldAutorotate replaces call to method shouldAutorotateToInterfaceOrientation (deprecated)
-(BOOL)shouldAutorotate
{ // check whether selected view controller should autorotate
UIViewController *controller = self.selectedViewController;
if ([controller isKindOfClass:[UINavigationController class]])
{ // in case it is a navigation controller: get visible view of that
controller = [(UINavigationController *)controller visibleViewController];
}
return [controller shouldAutorotate];
}
感谢你,谢谢你,谢谢你。这已经2天搞清楚如何做到这一点。下面是我对您的所有很大的帮助,当你有一个navigationControllers tabBarController。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
UIViewController *controller = self.selectedViewController;
if ([controller isKindOfClass:[UINavigationController class]])
controller = [(UINavigationController *)controller visibleViewController];
if([controller isKindOfClass:[LOCviewcontroller class]])
return YES;
else
if([controller isKindOfClass:[personWebSiteView class]])
return YES;
else return NO;
}
的编码器neophite的代码的任何批判总是赞赏...插孔
难道真的确定子类的UITabBarController(如上面的接受的答案建议)?
我了解,苹果说像“你应该永远继承的UITabBarController UINavigationController的或” - ?或有我误解了