将 iPhone 设备旋转至横向时隐藏 TabBar

发布于 2024-07-27 05:26:44 字数 765 浏览 5 评论 0原文

这就是我所拥有的: 处理不同 UIViewController 的 UITabBarController。 在 UIViewController 之一中,我试图在设备旋转到横向时切换显示的视图。 重要的是,横向显示的视图必须占据整个屏幕...

我已经正确实现了这些方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

事实上,我的旋转确实正确,并且我的视图交换了。 我什至隐藏状态栏,导航栏和选项卡栏,但我在屏幕底部仍然有一个空白区域,这是选项卡栏的位置...

所以我假设设置选项卡栏的隐藏属性是不够的为了在整个屏幕上查看。 我认为在 TabBarController 甚至 MainWindow 中需要做一些事情来表达“我现在不需要 TabBarController”之类的内容。 但我不知道如何正确解决这个问题。

如果有人遇到过这个问题,我将不胜感激。

谢谢你, 萨米。

So here is what i have:
A UITabBarController that handles different UIViewControllers. In one of the UIViewController i am trying to switch the view being displayed when the device rotates to landscape.
the important part is that the view displayed in landscape MUST take the whole screen...

I have correctly implemented the methods :

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

In fact i do have my rotation occurring correctly, and I my views swaped.
i even hide status bar, nav bar and Tab bar BUT i keep having a blank space at the bottom of the screen which is the place of the TabBar...

So i am assuming that setting the hidden property of the tabBar is not enough in order to have the view on the whole screen. I think there is some stuff to do within the TabBarController or even the MainWindow to say somehting like "i don't need TabBarController now". But i do not see how to get around this problem properly.

If anyone has been around this issue, i would appreciate some help.

thank you,
Sami.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

请你别敷衍 2024-08-03 05:26:44

这对我有用。


- (void)viewDidLoad {
    [super viewDidLoad];    
    previousRect = self.view.frame; 
}

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

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {       
        [self.navigationController setNavigationBarHidden:TRUE animated:FALSE]; 
        [[UIApplication sharedApplication] setStatusBarHidden:TRUE animated:FALSE];
    }
    else
    {
        [self.navigationController setNavigationBarHidden:FALSE animated:FALSE];
        [[UIApplication sharedApplication] setStatusBarHidden:FALSE animated:FALSE];
    }
}

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    UIInterfaceOrientation toOrientation = self.interfaceOrientation;

    if ( self.tabBarController.view.subviews.count >= 2 )
    {
        UIView *transView = [self.tabBarController.view.subviews objectAtIndex:0];
        UIView *tabBar = [self.tabBarController.view.subviews objectAtIndex:1];

        if(toOrientation == UIInterfaceOrientationLandscapeLeft || toOrientation == UIInterfaceOrientationLandscapeRight) {                 
            transView.frame = CGRectMake(0, 0, 480, 320 );
            tabBar.hidden = TRUE;
        }
        else
        {               
            transView.frame = previousRect;     
            tabBar.hidden = FALSE;
        }
    }
}

This worked for me.


- (void)viewDidLoad {
    [super viewDidLoad];    
    previousRect = self.view.frame; 
}

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

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {       
        [self.navigationController setNavigationBarHidden:TRUE animated:FALSE]; 
        [[UIApplication sharedApplication] setStatusBarHidden:TRUE animated:FALSE];
    }
    else
    {
        [self.navigationController setNavigationBarHidden:FALSE animated:FALSE];
        [[UIApplication sharedApplication] setStatusBarHidden:FALSE animated:FALSE];
    }
}

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    UIInterfaceOrientation toOrientation = self.interfaceOrientation;

    if ( self.tabBarController.view.subviews.count >= 2 )
    {
        UIView *transView = [self.tabBarController.view.subviews objectAtIndex:0];
        UIView *tabBar = [self.tabBarController.view.subviews objectAtIndex:1];

        if(toOrientation == UIInterfaceOrientationLandscapeLeft || toOrientation == UIInterfaceOrientationLandscapeRight) {                 
            transView.frame = CGRectMake(0, 0, 480, 320 );
            tabBar.hidden = TRUE;
        }
        else
        {               
            transView.frame = previousRect;     
            tabBar.hidden = FALSE;
        }
    }
}

墨落画卷 2024-08-03 05:26:44
  • 上面的解决方案也对我有用,只是对 iOS6 及更高版本进行了一些小更改:
  • 在 iOS6 中删除行:“ previousRect = self.view.frame; ”
  • 还将“animated:”替换为“ withAnimation:”
  • 并删除“ transView.frame = previousRect; " 从底部(在 else 函数中)
  • 这样对我有用。 非常感谢用户 UB。
  • The solution above worked for me too just some little changes for iOS6 and above:
  • in iOS6 remove the line: " previousRect = self.view.frame; "
  • also replace " animated: " with " withAnimation: "
  • and remove " transView.frame = previousRect; " from the bottom (in else function)
  • It works for me this way. And a big thanks to user UB.
如果没有 2024-08-03 05:26:44

这段代码工作正常,但是当我关闭以模态方式呈现的 uiviewcontroller 时,我的视图位于状态栏下方 20 像素。
我的视图位于导航控制器内,因此我在旋转之前不会隐藏它。

This code works fine but when i dismiss a uiviewcontroller which is presented modally, my view is under the status bar by 20 pixel.
My view is inside a navigationcontroller so i do not hide it before rotation.

难以启齿的温柔 2024-08-03 05:26:44

我需要标签栏在横向视图中进入全屏模式,我尝试了上面建议的方法,使用

transView.frame = CGRectMake(0, 0, 480, 320 );

这结果是一个 hacky解决方案并提出了许多问题,例如隐藏和重新显示状态栏(退出纵向视图后重新显示时,视图会与状态栏重叠)。 我不会推荐这个。 最终对我来说完美的方法是推送一个包含横向视图的新视图控制器,并使用委托来重用原始 VC 的功能。

I needed tab bar to go into full screen mode in landscape view and I tried the approach suggested above using

transView.frame = CGRectMake(0, 0, 480, 320 );

This turned out to be a hacky solution and posed many problems such as with hiding and re-displaying the status bar (the view would overlap with the status bar when it is re-displayed after exiting portrait view). I would not recommend this. What worked for me perfectly in the end was pushing a new view controller containing the landscape view and using delegation to reuse the functionality of the original VC.

煞人兵器 2024-08-03 05:26:44

这种方法对我有用:(

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    UIView *parentView = self.tabBarController.view;
    CGRect frame = parentView.frame;
    CGFloat windowHeight = parentView.window.frame.size.height;

    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
            frame.size.height = windowHeight + tabBarHeight;
            break;
        default:
            frame.size.height = windowHeight;
            break;
    }

    [UIView animateWithDuration:duration animations:^{
        parentView.frame = frame;
    }];
}

仅在 iOS8 中测试。)

This approach is working for me:

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    UIView *parentView = self.tabBarController.view;
    CGRect frame = parentView.frame;
    CGFloat windowHeight = parentView.window.frame.size.height;

    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
            frame.size.height = windowHeight + tabBarHeight;
            break;
        default:
            frame.size.height = windowHeight;
            break;
    }

    [UIView animateWithDuration:duration animations:^{
        parentView.frame = frame;
    }];
}

(Only tested in iOS8.)

不顾 2024-08-03 05:26:44

子类化您的 TabBarController 并在需要时隐藏 TabBar:

class tabBarVC: UITabBarController {

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        if size.height < size.width {
            self.tabBar.hidden = true
        } else {
            self.tabBar.hidden = false
        }
    }

}

Subclass your TabBarController and hide the TabBar when needed:

class tabBarVC: UITabBarController {

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        if size.height < size.width {
            self.tabBar.hidden = true
        } else {
            self.tabBar.hidden = false
        }
    }

}
橘亓 2024-08-03 05:26:44

也许你想使用这个

- (void)willAnimateRotationToInterfaceOrientation:UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

    __block UIView *weakTabBar = [self.tabBarController.view.subviews objectAtIndex:1];
    weakTabBar.alpha = 0;
    [UIView animateWithDuration:duration
                          delay:0
                        options:UIViewAnimationOptionCurveEaseIn // slow at the beggining
                     animations:^{
                         weakTabBar.alpha = 1;
                     }
                     completion:^(BOOL finished) {
                         weakTabBar.alpha = 1;
                     }];
    }

}

这不会隐藏标签栏,但会使旋转动画更平滑。

Maybe you want to use this

- (void)willAnimateRotationToInterfaceOrientation:UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

    __block UIView *weakTabBar = [self.tabBarController.view.subviews objectAtIndex:1];
    weakTabBar.alpha = 0;
    [UIView animateWithDuration:duration
                          delay:0
                        options:UIViewAnimationOptionCurveEaseIn // slow at the beggining
                     animations:^{
                         weakTabBar.alpha = 1;
                     }
                     completion:^(BOOL finished) {
                         weakTabBar.alpha = 1;
                     }];
    }

}

This doesn't hide the tab bar but makes the rotate animation smoother.

允世 2024-08-03 05:26:44

如果您有 UITabBarController 然后将 UINavigationController 放入其中,那么您可以使用 hidesBottomBarWhenPushed (带有一点技巧)来执行此操作。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        self.hidesBottomBarWhenPushed = NO;
        self.navigationController.viewControllers = self.navigationController.viewControllers;
        [self transitionToGridLayout];
    }
    else {
        self.hidesBottomBarWhenPushed = YES;
        self.navigationController.viewControllers = self.navigationController.viewControllers;
        [self transitionToCoverflowLayout];
    }
}

诀窍是推动视图控制器,以便拾取 hidesBottomBarWhenPushed 标志。 您可以使用以下。

self.navigationController.viewControllers = self.navigationController.viewControllers;

If you have your UITabBarController then put a UINavigationController inside it then you can use hidesBottomBarWhenPushed (with a bit of trickery) to do this.

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        self.hidesBottomBarWhenPushed = NO;
        self.navigationController.viewControllers = self.navigationController.viewControllers;
        [self transitionToGridLayout];
    }
    else {
        self.hidesBottomBarWhenPushed = YES;
        self.navigationController.viewControllers = self.navigationController.viewControllers;
        [self transitionToCoverflowLayout];
    }
}

The trick is to push your view controller so that the hidesBottomBarWhenPushed flag is picked up. You can use following.

self.navigationController.viewControllers = self.navigationController.viewControllers;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文