简体   繁体   English

如何实现隐藏UITabBar以在iOS 11和iPhone X上显示UIToolbar

[英]How to implement hiding the UITabBar to display an UIToolbar on iOS 11 and iPhone X

I'm trying to do an UI similar to the Photos app, where when you enter in a selection mode that hides the tab bar to display a toolbar. 我正在尝试做一个与“照片”应用类似的UI,在这种情况下,当您以选择模式进入时会隐藏标签栏以显示工具栏。

I have my view controller in a UINavigationController and the navigation controller in a UITabBarController. 我在UINavigationController中有视图控制器,在UITabBarController中有导航控制器。

I had other strategies before but I'm struggling to get this working on the iPhone X and its bottom safe margins. 之前我有其他策略,但我正在努力使其在iPhone X及其底部安全边距上运行。

If I'm making the correct assumptions based on your description of the Photos App, I think you may be confused as to what the app is doing behind the scenes when going from Photos App TabBar to Photos App Toolbar . 如果我根据您对Photos App的描述做出正确的假设,那么当您从Photos App TabBar转到Photos App Toolbar时,我可能会对应用程序在后台执行的操作感到困惑。

These are two different ViewControllers, the second only shows the toolbar and sets hidesBottomBarWhenPushed = true in the init. 这是两个不同的ViewController,第二个仅显示工具栏,并在init中设置hidesBottomBarWhenPushed = true You can use the NavigationController's supplied toolbar by setting the setToolbarItems(toolbarItems: [UIBarButtonItem]?, animated: Bool) in your second ViewController. 您可以通过在第二个ViewController中设置setToolbarItems(toolbarItems: [UIBarButtonItem]?, animated: Bool)来使用NavigationController提供的工具栏。 This properly sizes the toolbar in the view to account for the bottom control on the iPhoneX. 这样可以正确调整视图中工具栏的大小,以说明iPhoneX的底部控件。

If you must manage toolbar and TabBar visibility in the same ViewController, based on my testing, you'll need to add/manage the toolbar manually within a UIView container to get the proper size on all devices. 根据我的测试,如果必须在同一ViewController中管理工具栏和TabBar可见性,则需要在UIView容器中手动添加/管理工具栏,以在所有设备上获得合适的尺寸。 So the view hierarchy would be ViewController.view -> toolbarContainer View -> Toolbar. 因此,视图层次结构为ViewController.view-> toolbarContainer View-> Toolbar。

for iPhone X, the tab bar height is different than iPhone 8, you need to track 对于iPhone X,标签栏高度与iPhone 8不同,您需要跟踪

static CGFloat tabBarMaxHeight = 0;

- (void)setToolbarHidden:(BOOL)hidden {
    [self.navigationController setToolbarHidden:hidden animated:NO];
    CGRect frame = self.tabBarController.tabBar.frame;
    tabBarMaxHeight = MAX(tabBarMaxHeight, CGRectGetHeight(frame));
    frame.size.height = hidden ? tabBarMaxHeight : 0;
    self.tabBarController.tabBar.frame = frame;
    self.tabBarController.tabBar.hidden = !hidden;

    //! reset tab bar item title to prevent text style compacted
    for (UITabBarItem *obj in self.tabBarController.tabBar.items) {
         NSString *title = obj.title;
         obj.title = @"";
         obj.title = title;
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM