修复 iOS12.1 UITabBar 布局位移bug

Bug触发条件
1.使用 UITabBarController + UINavigationController 组合
2.UITabBar带半透明效果,isTranslucent 属性为 YES
3.UIViewController的 hidesBottomBarWhenPushed 属性为 YES
4.通过导航栏返回上一页时(导航栏返回按钮 or 屏幕左侧的滑动返回手势)
在这里插入图片描述

解决方案

@interface RCDTabBarBtn : UIView

@end

@implementation RCDTabBarBtn

+ (void)load {
    if (@available(iOS 12.1, *)) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            Class originalClass = NSClassFromString(@"UITabBarButton");
            SEL originalSelector = @selector(setFrame:);
            SEL swizzledSelector = @selector(xp_setFrame:);
            
            Method originalMethod = class_getInstanceMethod(originalClass, originalSelector);
            Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
            class_replaceMethod(originalClass,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
            class_replaceMethod(originalClass,
                                originalSelector,
                                method_getImplementation(swizzledMethod),
                                method_getTypeEncoding(swizzledMethod));
        });
    }
}

- (void)xp_setFrame:(CGRect)frame {
    if (!CGRectIsEmpty(self.frame)) {
        // for iPhone 8/8Plus
        if (CGRectIsEmpty(frame)) {
            return;
        }
        // for iPhone XS/XS Max/XR
        frame.size.height = MAX(frame.size.height, 48.0);
    }
    [self xp_setFrame:frame];
}

@end

不知为何,在非刘海屏机型上,frame 的 size 为 {0, 0},但是在刘海屏上却不是这个值,而是高度为 33.0 的尺寸(也不确定这个值是否固定为33.0)。

使用
直接将代码拷贝到项目即可,无需进行任何方法调用。

转载:http://www.0daybug.com/2018/11/02/iOS12.1-UITabBar/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值