代码之家  ›  专栏  ›  技术社区  ›  Kapil_A

选项卡栏控制器中的图像和标题是否重叠?

  •  0
  • Kapil_A  · 技术社区  · 6 年前

    选项卡栏控制器中的选项卡栏项在iPhoneX中重叠。 有什么解决办法吗? enter image description here

    2 回复  |  直到 6 年前
        1
  •  1
  •   Kapil_A    6 年前
    • 发现与动画相关的问题。
    • 我能解决这个问题。
    • 通过使用CustomTabBarClass对Interface builder中的UITabBar进行子分类

    -班级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
            }
        }
    }
    

    -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
    
        2
  •  0
  •   Subhash Sharma    6 年前

    我不认为这是一个完美的答案,但下面的代码解决了我的问题。

    1. 为tabbarcontroller创建子类
    2. 添加代码 视图加载

      宽度=tabBar.bounds.width标签

      if UIDevice().userInterfaceIdiom == .phone {
          switch UIScreen.main.nativeBounds.height {
      
          case 2436:
              let tabSize = CGSize(width: width/5, height: 83)
              UIGraphicsBeginImageContext(tabSize)
              selectionImage?.draw(in: CGRect(x: 0, y: 0, width: tabSize.width, height: tabSize.height))
              selectionImage = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext()
      
      
          default:
              let tabSize = CGSize(width: width/5, height: 49)
              UIGraphicsBeginImageContext(tabSize)
              selectionImage?.draw(in: CGRect(x: 0, y: 0, width: tabSize.width, height: tabSize.height))
              selectionImage = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext()
      
      
          }
      }