【问题标题】:Disable rotation for one UITabbar item禁用一个 UITabbar 项目的旋转
【发布时间】:2014-02-19 16:46:47
【问题描述】:

我有一个带有 4 个标签栏项目的 uitabbarcontroller,每个标签栏项目都有一个 uinavigationcontroller。

我只需要将一个 uitabbar 项目的方向锁定为纵向。所以我实现了以下代码:

创建了一个自定义标签栏控制器,并在其中添加了以下代码:

MainTabBarController.m

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

-(NSUInteger)supportedInterfaceOrientations
{
    if (self.selectedViewController)
        return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

创建了一个自定义导航控制器以在其中一个 uitabbaritems 中使用,并在其中添加了以下代码:

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

-(BOOL)shouldAutorotate
{
    return YES;
}

对于自定义导航控制器中的 uiviewcontroller,我添加了以下代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

上面的代码工作正常。我的问题是,如果您在设备已经处于横向模式时转到选项卡栏项(其方​​向锁定为 Protrait),则方向将更改为横向。谁能帮我解决我的问题。

谢谢, 阿南德。

【问题讨论】:

    标签: uitabbarcontroller orientation landscape uitabbaritem portrait


    【解决方案1】:

    仅供参考!!! 我找到了解决我的问题的方法。我在视图控制器中添加了以下功能,我希望仅在纵向模式下显示:

    -(void)viewWillLayoutSubviews
    {
        [super viewWillLayoutSubviews];
        if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]))
        {
            if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
            {
                int orientationPortrait = UIInterfaceOrientationPortrait;
                NSMethodSignature *sig = [[UIDevice currentDevice] methodSignatureForSelector:@selector(setOrientation:)];
                NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig];
                [invo setTarget:[UIDevice currentDevice]];
                [invo setSelector:@selector(setOrientation:)];
                [invo setArgument:&orientationPortrait atIndex:2];
                [invo invoke];
            }
        }
    }
    

    【讨论】:

    • 我看到从纵向变为横向时有一个小动画。有没有办法将此动画设置为 NO?
    • @justME 抱歉,我不知道如何将该动画设置为 NO。如果你发现了,请告诉我。
    • 这不是一个很好的解决方案,视图仍然会旋转,但会翻转回纵向。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多