【问题标题】:How to rotate a view inside the tab bar to Portrait如何将标签栏内的视图旋转为纵向
【发布时间】:2014-02-22 00:03:49
【问题描述】:

[我搜索了 Stackoverflow 并尝试了各种解决方案/建议,但尚未解决此问题。如果有人能指出我在这里缺少什么,非常感谢。]

我有一个带有多个选项卡的自定义 UITabBarController。每个都是一个 UINavigationController。其中一个 ViewX 是自定义 UINavigationController。

我希望 ViewX 不旋转为横向(仅支持 PortraitUp)。我能够做到这一点。但是,如果我在其他选项卡上处于横向模式并使用 ViewX 点击选项卡,我希望它旋转回纵向,但它保持横向。

CustomUITabController 有这样的代码:

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

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

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

    return UIInterfaceOrientationMaskPortrait;
}

CustomUINavigationController 实现了这个:

-(BOOL)shouldAutorotate
{
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
    return (interfaceOrientation != UIInterfaceOrientationPortrait);
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

我什至尝试过:这只会将状态栏旋转为纵向,但视图仍保持为横向。

- (void)viewWillLayoutSubviews
{
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

    if (interfaceOrientation != UIInterfaceOrientationPortrait)
    {
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
    }
}

【问题讨论】:

    标签: ios iphone objective-c ios7 uitabbarcontroller


    【解决方案1】:

    我使用以下方法强制只为一个视图设置纵向方向,它对我的​​工作,可能会对你有所帮助

    1、创建一个全局标志

    delegate.flagOrientationAll 
    

    2,在您的视图控制器的.m 中添加以下代码,用于您的项目ViewX.m

    // set flag "flagOrientationAll" to rotate only one view in your perticular view 
    -(void)viewWillAppear:(BOOL)animated
    {
        NSLog (@"webViewController -- viewWillAppear");
        [super viewWillAppear:animated];
    
        PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *) [[UIApplication sharedApplication] delegate];
        delegate.flagOrientationAll = YES;
    }
    
    -(void)viewWillDisappear:(BOOL)animated
    {
        NSLog (@"webViewController -- viewWillDisappear");
        PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *)[[UIApplication sharedApplication] delegate];
        delegate.flagOrientationAll = NO;
    
    }
    

    3.对于其他视图,您必须设置

    delegate.flagOrientationAll = NO;
    

    4、在你的应用代理中添加自动旋转方法

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
        //NSLog(@"AppDelegate -- supportedInterfaceOrientationsForWindow");
        if(flagOrientationAll == YES){
            return UIInterfaceOrientationMaskPortrait;
        }else {
            return UIInterfaceOrientationMaskAll;
        } 
    }  
    

    这是我的帖子:How to set one of the screens in landscape mode in iphone?

    【讨论】:

    • 感谢您的意见。不幸的是,它也没有工作。该解决方案类似于我之前的解决方案,但只是在 appDelegate 级别。我仍然尝试过,但没有奏效。 application:supportedInterfaceOrientationsForWindow: 被调用,我看到返回的值是 UIInterfaceOrientationMaskPortrait 但这没有帮助。问题是如果我的设备处于横向模式,我在另一个选项卡中,视图也在横向,现在我切换到这个选项卡,我希望这个带有 ViewX 的选项卡旋转到纵向。
    猜你喜欢
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多