OGeek|极客世界-中国程序员成长平台

标题: ios - TabBarController : Orienting views in different orientations [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:32
标题: ios - TabBarController : Orienting views in different orientations

我无法保持当前的 View 方向。在下面的设置中,我已经能够将第一个 View Controller 锁定为纵向,将第二个 View Controller 锁定为横向或纵向。但是,当我将第二个导航 Controller /rootviewcontroller 添加到选项卡 Controller 时,整个项目的所有 View 都将变为横向和纵向。无论我是否在第一个导航 Controller 到第二个导航 Controller 中实现相同的代码,都会发生这种情况

我希望能够保留我当前的 View Controller 方向,同时添加一个额外的 navcontroller>viewcontroller

我在 Storyboard 中有以下设置: enter image description here

这就是我想要实现的目标: enter image description here

tabbarcontroller应该支持所有方向,nav Controller 支持所有方向,第一个 View Controller 和表格 View Controller 只支持纵向,第二个 View Controller 支持横向和纵向。

这里是每个当前 View Controller 的方法

TabViewController.m

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

NavController.m

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

First View Controller.m

-(BOOL)shouldAutorotate

{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{

    return UIInterfaceOrientationPortrait;
}

第二 View Controller .m

-(BOOL)shouldAutorotate

{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{

    return UIInterfaceOrientationLandscapeLeft;
}

在解决此问题时,我将不胜感激。它已成为我存在的祸根



Best Answer-推荐答案


您似乎已经将导航和标签栏 Controller 子类化了。您可以在运行时使用它们的委托(delegate)协议(protocol)来控制方向,而不是这样做。

– tabBarControllerSupportedInterfaceOrientations:
– tabBarControllerPreferredInterfaceOrientationForPresentation:

– navigationControllerPreferredInterfaceOrientationForPresentation:
– navigationControllerSupportedInterfaceOrientations:

你可以把你的逻辑放在那些我认为有一个更清晰的实现而不是子类化的方法中。容器 View Controller 通常接管单个 vc 旋转实现。

关于ios - TabBarController : Orienting views in different orientations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24479184/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4