禁用一项UITabbar项目的旋转 [英] Disable rotation for one UITabbar item

查看:87
本文介绍了禁用一项UITabbar项目的旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a uitabbarcontroller with 4 tab bar items and each tab bar item has a uinavigationcontroller.

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

I needed to lock the orientation of one uitabbar item only to Portrait. So i implemented the following code:

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

Created a custom tab bar controller and added the following code in it:

MainTabBarController.m

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的自定义导航控制器,并在其中添加了以下代码:

Created a custom navigation controller to use in one of the uitabbaritems and added the following code in it:

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

-(BOOL)shouldAutorotate
{
    return YES;
}

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

and for the uiviewcontroller in the custom navigation controller i added the following code:

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

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

上面的代码工作正常.我的问题是,如果设备已处于横向模式时转到选项卡项(其方向已锁定为Protrait),则方向会变为横向.谁能帮我解决问题.

The above code works fine. My issue is, if you go to the tabbar item (whose orientation is locked to Protrait) when the device is already in Landscape mode the orientation changes to Landscape. Can anyone please help me how to solve my issue.

谢谢, 阿南德.

推荐答案

仅供参考! 我已经找到解决问题的方法.我将以下功能添加到只希望以protrait模式显示的视图控制器中:

FYI!!! I've found a way for my problem. I added the following function to the viewcontroller for which i want the display only in protrait mode:

-(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];
        }
    }
}

这篇关于禁用一项UITabbar项目的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆