Search code examples
iosautolayoutiphone-x

Image and Title in Tab Bar Controller get’s overlap iPhone X?


Tab Bar Item inside Tab Bar Controller get’s overlap in iPhone X. Any Solution for this ? enter image description here


Solution

    • Issue found to be related to animation.
    • I was able to resolve this issue.
    • By sub classing UITabBar from Interface builder with CustomTabBarClass

    Swift Code - Class CustomTabBarClass.swift

    class CustomTabBarClass: 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 < 90) {
                size.height += bottomInset
            }
        }
        return size
    }
    
    override var frame: CGRect {
        get {
            return super.frame
        }
        set {
            var tmp = newValue
            if let superview = superview, tmp.maxY != 
            superview.frame.height {
                tmp.origin.y = superview.frame.height - tmp.height
            }
    
            super.frame = tmp
            }
        }
    }
    

    Objective C Code - CustomTabBarClass.m

    @implementation CustomTabBarClass{
        UIEdgeInsets oldSafeAreaInsets;
    }
    
    
    - (void)awakeFromNib {
        [super awakeFromNib];
    
        oldSafeAreaInsets = UIEdgeInsetsZero;
    }
    
    
    - (void)safeAreaInsetsDidChange {
        [super safeAreaInsetsDidChange];
    
        if (!UIEdgeInsetsEqualToEdgeInsets(oldSafeAreaInsets, self.safeAreaInsets)) {
            [self invalidateIntrinsicContentSize];
    
            if (self.superview) {
                [self.superview setNeedsLayout];
                [self.superview layoutSubviews];
            }
        }
    }
    
    - (CGSize)sizeThatFits:(CGSize)size {
        size = [super sizeThatFits:size];
    
        if (@available(iOS 11.0, *)) {
            float bottomInset = self.safeAreaInsets.bottom;
            if (bottomInset > 0 && size.height < 50 && (size.height + bottomInset < 90)) {
                size.height += bottomInset;
            }
        }
    
        return size;
    }
    
    
    - (void)setFrame:(CGRect)frame {
        if (self.superview) {
            if (frame.origin.y + frame.size.height != self.superview.frame.size.height) {
                frame.origin.y = self.superview.frame.size.height - frame.size.height;
            }
        }
        [super setFrame:frame];
    }
    
    
    @end