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

前端 未结 30 2446
误落风尘
误落风尘 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:39

    Fixed by using subclassed UITabBar to apply safeAreaInsets:

    class SafeAreaFixTabBar: UITabBar {
    
        var oldSafeAreaInsets = UIEdgeInsets.zero
    
        @available(iOS 11.0, *)
        override func safeAreaInsetsDidChange() {
            super.safeAreaInsetsDidChange()
    
            if oldSafeAreaInsets != safeAreaInsets {
                oldSafeAreaInsets = safeAreaInsets
    
                invalidateIntrinsicContentSize()
                superview?.setNeedsLayout()
                superview?.layoutSubviews()
            }
        }
    
        override func sizeThatFits(_ size: CGSize) -> CGSize {
            var size = super.sizeThatFits(size)
            if #available(iOS 11.0, *) {
                let bottomInset = safeAreaInsets.bottom
                if bottomInset > 0 && size.height < 50 {
                    size.height += bottomInset
                }
            }
            return size
        }
    }
    
    0 讨论(0)
  • 2020-11-29 17:39

    I had the similar problem, at first it was rendered correctly but after setting up badgeValue on one of the tabBarItem it broke the layout. What it worked for me without subclassing UITabBar was this, on my already created UITabBarController subclass.

    -(void)viewDidLayoutSubviews {
        [super viewDidLayoutSubviews];
        NSLayoutYAxisAnchor *tabBarBottomAnchor = self.tabBar.bottomAnchor;
        NSLayoutYAxisAnchor *tabBarSuperviewBottomAnchor = self.tabBar.superview.bottomAnchor;
        [tabBarBottomAnchor constraintEqualToAnchor:tabBarSuperviewBottomAnchor].active = YES;
        [self.view layoutIfNeeded];
    }
    

    I used tabBar superview to make sure that the constraints/anchors are on the same view hierarchy and avoid crashes.

    Based on my understanding, since this seems to be a UIKit bug, we just need to rewrite/re-set the tab bar constraints so the auto layout engine can layout the tab bar again correctly.

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

    If you have any height constraint for the Tab Bar try removing it .

    Faced the same problem and removing this solved the issue.

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

    I was having the same issue when I was trying to set the frame of UITabBar in my custom TabBarController.

    self.tabBar.frame = CGRectMake(0, self.view.frame.size.height - kTabBarHeight, self.view.frame.size.width, kTabBarHeight);
    

    When I just adjusted it to the new size the issue went away

    if(IS_IPHONE_X){
        self.tabBar.frame = CGRectMake(0, self.view.frame.size.height - kPhoneXTabBarHeight, self.view.frame.size.width, kPhoneXTabBarHeight);
    }
    else{
        self.tabBar.frame = CGRectMake(0, self.view.frame.size.height - kTabBarHeight, self.view.frame.size.width, kTabBarHeight);
    }
    
    0 讨论(0)
  • 2020-11-29 17:46

    Moving the tab bar 1 point away from the bottom worked for me.

    Of course you'll get a gap by doing so which you'll have to fill in some way, but the text/icons are now properly positioned.

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

    I was having the same issue which was solved by setting the items of the tabBar after the tab bar was laid out.

    In my case the issue happened when:

    1. There is a custom view controller
    2. A UITabBar is created in the initializer of the view controller
    3. The tab bar items are set before view did load
    4. In view did load the tab bar is added to the main view of the view controller
    5. Then, the items are rendered as you mention.
    0 讨论(0)
提交回复
热议问题