iOS 11 iPhone X simulator UITabBar icons and titles being rendered on top covering eachother

前端 未结 30 2445
误落风尘
误落风尘 2020-11-29 17:05

Anyone having issue with the iPhone X simulator around the UITabBar component?

Mine seem to be rendering the icons and title on top of each other, I\'m not sure if I

相关标签:
30条回答
  • 2020-11-29 17:47

    The UITabBar is increasing in height to be above the home button/line, but drawing the subview in its original location and overlaying the UITabBarItem over the subview.

    As a workaround you can detect the iPhone X and then shrink the height of the view by 32px to ensure the tab bar is displayed in the safe area above the home line.

    For example, if you're creating your TabBar programatically replace

    self.tabBarController = [[UITabBarController alloc] init];
    self.window.rootViewController = self.tabBarController;
    

    With this:

    #define IS_IPHONEX (([[UIScreen mainScreen] bounds].size.height-812)?NO:YES)
    self.tabBarController = [[UITabBarController alloc] init];    
    self.window.rootViewController = [[UIViewController alloc] init] ;
    if(IS_IPHONEX)
        self.window.rootViewController.view.frame = CGRectMake(self.window.rootViewController.view.frame.origin.x, self.window.rootViewController.view.frame.origin.y, self.window.rootViewController.view.frame.size.width, self.window.rootViewController.view.frame.size.height + 32) ;
    [self.window.rootViewController.view addSubview:self.tabBarController.view];
    self.tabBarController.tabBar.barTintColor = [UIColor colorWithWhite:0.98 alpha:1.0] ;
    self.window.rootViewController.view.backgroundColor = [UIColor colorWithWhite:0.98 alpha:1.0] ;
    

    NOTE: This could well be a bug, as the view sizes and tab bar layout are set by the OS. It should probably display as per Apple's screenshot in the iPhone X Human Interface Guidelines here: https://developer.apple.com/ios/human-interface-guidelines/overview/iphone-x/

    0 讨论(0)
  • 2020-11-29 17:50

    For those who write whole UITabBarController programmatically, you can use UITabBarItem.appearance().titlePositionAdjustment to adjust the title position

    So in this case that you want add a gap between Icon and Title use it in viewDidLoad:

        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Specify amount to offset a position, positive for right or down, negative for left or up
            let verticalUIOffset = UIOffset(horizontal: 0, vertical: hasTopNotch() ? 5 : 0)
    
            UITabBarItem.appearance().titlePositionAdjustment = verticalUIOffset
        }
    

    detecting if device has Notch screen:

      func hasTopNotch() -> Bool {
          if #available(iOS 11.0, tvOS 11.0, *) {
            return UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0 > 20
          }
          return false
      }
    
    0 讨论(0)
  • 2020-11-29 17:51

    For iPhone you can do this, Subclass UITabBarController.

    class MyTabBarController: UITabBarController {
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if #available(iOS 11, *) {
            self.tabBar.heightAnchor.constraint(equalToConstant: 40).isActive = true
            self.tabBar.invalidateIntrinsicContentSize()
        }
      }
    }
    

    Goto Storyboard and allow Use Safe Area Layout Guide and change class of UITabbarController to MyTabBarController

    P.S This solution is not tested in case of universal application and iPad.

    0 讨论(0)
  • 2020-11-29 17:51

    try to change splash screen with @3x size is (3726 × 6624)

    0 讨论(0)
  • 2020-11-29 17:51

    For me this fixed all the issues:

        override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            let currentHeight = tabBar.frame.height
            tabBar.frame = CGRect(x: 0, y: view.frame.size.height - currentHeight, width: view.frame.size.width, height: currentHeight)
        }
    
    0 讨论(0)
  • 2020-11-29 17:53

    I have scratched my head over this problem. It seems to be associated with how the tabBar is initialized and added to view hierarchy. I also tried above solutions like calling invalidateIntrinsicContentSize, setting the frame, and also bottomInsets inside a UITabBar subclass. They seem to work however temporarily and they break of some other scenario or regress the tab bar by causing some ambiguous layout issue. When I was debugging the issue I tried assigning the height constraints to the UITabBar and centerYAnchor, however neither fixed the problem. I realized in view debugger that the tabBar height was correct before and after the problem reproduced, which led me to think that the problem was in the subviews. I used the below code to successfully fix this problem without regressing any other scenario.

    - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
    {
        [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
        if (DEVICE_IS_IPHONEX())
        {
            [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
                for (UIView *view in self.tabBar.subviews)
                {
                    if ([NSStringFromClass(view.class) containsString:@"UITabBarButton"])
                    {
                        if (@available (iOS 11, *))
                        {
                            [view.bottomAnchor constraintEqualToAnchor:view.superview.safeAreaLayoutGuide.bottomAnchor].active = YES;
                        }
                    }
                }
            } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
                [self.tabBar layoutSubviews];
            }];
        }
    }
    

    Assumptions: I am doing this only for iPhone X, since it doesn't seem to reproduce on any other device at the moment. Is based on the assumption that Apple doesn't change the name of the UITabBarButton class in future iOS releases. We're doing this on UITabBarButton only when means if apple adds more internal subviews in to UITabBar we might need to modify the code to adjust for that.

    Please lemme know if this works, will be open to suggestions and improvements!

    It should be simple to create a swift equivalent for this.

    0 讨论(0)
提交回复
热议问题