美文网首页
iOS tabbar高度修改(非复制)

iOS tabbar高度修改(非复制)

作者: iOS阿能 | 来源:发表于2021-11-13 11:34 被阅读0次
    image.png
    #import <objc/runtime.h>
    // 需要自定义DFTabbar,这些代码是写在自定义的TabbarVC里面
    #import "DFTabbar.h"
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self replaceTab];
    });
    
    - (void)replaceTab{
        //使用运行时替换系统的
           object_setClass(self.tabBar, [DFTabbar class]);
    
           //去掉原生tabbar分割线
           [self.tabBar setShadowImage:[UIImage new]];
           [self.tabBar setBackgroundImage:[UIImage new]];
    }
    
    
    #import "DFTabbar.h"
    
    /** 增加TabBar的高度 7 */
    static CGFloat increaseTabBarHeight = 7;
    
    @implementation DFTabbar
    
    -(instancetype)initWithFrame:(CGRect)frame{
      if (self = [super initWithFrame:frame]) {
          self.backgroundColor = [UIColor whiteColor];
      }
      return self;
    }
    
    //设置TabBar的子视图颜色
    -(void)layoutSubviews{
      for (UIView *view in self.subviews) {
          if (![view isKindOfClass:[DFTabView class]]) {
              view.backgroundColor = [UIColor clearColor];
          }
      }
    }
    
    //调整高度的核心方法
    -(CGSize)sizeThatFits:(CGSize)size{
      
      CGSize sizeThatFits = [super sizeThatFits:size];
        
      //根据你的设计稿的需要,可以写成固定的值,我下面的写法是考虑到所有机型都在默认的高度上增加15
      sizeThatFits.height= sizeThatFits.height+increaseTabBarHeight;
    
      return sizeThatFits;
      
    }
    
    @end
    
    
    

    相关文章

      网友评论

          本文标题:iOS tabbar高度修改(非复制)

          本文链接:https://www.haomeiwen.com/subject/pnoazltx.html