Skip to content

Commit

Permalink
v1.17.21 [work-arounds]compatible for iPhoneX for issue #312
Browse files Browse the repository at this point in the history
work-arounds 方案,解决 iOS12.1上,iPhoneX 位置跳动的问题。
  • Loading branch information
ChenYilong committed Nov 5, 2018
1 parent 353f990 commit 2ec9659
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CYLTabBarController.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "CYLTabBarController"
s.version = "1.17.20"
s.version = "1.17.21"
s.summary = "Highly customizable tabBar and tabBarController for iOS"
s.description = "CYLTabBarController is iPad and iPhone compatible. Supports landscape and portrait orientations and can be used inside UINavigationController."
s.homepage = "https://github.com/ChenYilong/CYLTabBarController"
Expand Down
22 changes: 14 additions & 8 deletions CYLTabBarController/CYLTabBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#import "CYLConstants.h"
#import <objc/runtime.h>

static void *const CYLTabBarContext = (void*)&CYLTabBarContext;

/**
* 用 block 重写某个 class 的指定方法
* @param targetClass 要重写的 class
Expand All @@ -30,9 +32,6 @@
return YES;
}


static void *const CYLTabBarContext = (void*)&CYLTabBarContext;

@interface CYLTabBar ()

/** 发布按钮 */
Expand All @@ -49,23 +48,30 @@ @implementation CYLTabBar
#pragma mark -
#pragma mark - LifeCycle Method

static CGFloat const CYLIPhoneXTabbarButtonHeight = 48;
static CGFloat const CYLIPhoneXTabbarButtonSafeAreaHeight = 35;

+ (void)load {
/* 这个问题是 iOS 12.1 Beta 2 的问题,只要 UITabBar 是磨砂的,并且 push viewController 时 hidesBottomBarWhenPushed = YES 则手势返回的时候就会触发。
出现这个现象的直接原因是 tabBar 内的按钮 UITabBarButton 被设置了错误的 frame,frame.size 变为 (0, 0) 导致的。如果12.1正式版Apple修复了这个bug可以移除调这段代码(来源于QMUIKit的处理方式)*/
/* 这个问题是 iOS 12.1 的问题,只要 UITabBar 是磨砂的,并且 push viewController 时 hidesBottomBarWhenPushed = YES 则手势返回的时候就会触发。(来源于QMUIKit的处理方式)*/
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (@available(iOS 12.1, *)) {
OverrideImplementation(NSClassFromString(@"UITabBarButton"), @selector(setFrame:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP originIMP) {
return ^(UIView *selfObject, CGRect firstArgv) {

if ([selfObject isKindOfClass:originClass]) {

// 如果发现即将要设置一个 size 为空的 frame,则屏蔽掉本次设置
if (!CGRectIsEmpty(selfObject.frame) && CGRectIsEmpty(firstArgv)) {
return;
}

// 兼容 iOS 12 的 iPhoneX
CGFloat tabBarHeight = firstArgv.size.height;
CGFloat realTabBarHeight = CYLTabBarHeight ? (CYLTabBarHeight - CYLIPhoneXTabbarButtonSafeAreaHeight): CYLIPhoneXTabbarButtonHeight;
if (CYL_IS_IPHONE_X && (tabBarHeight != realTabBarHeight)) {
firstArgv.size.height = realTabBarHeight;
}
}

// call super
void (*originSelectorIMP)(id, SEL, CGRect);
originSelectorIMP = (void (*)(id, SEL, CGRect))originIMP;
Expand Down
1 change: 1 addition & 0 deletions CYLTabBarController/CYLTabBarController.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ FOUNDATION_EXTERN NSUInteger CYLTabbarItemsCount;
FOUNDATION_EXTERN NSUInteger CYLPlusButtonIndex;
FOUNDATION_EXTERN CGFloat CYLPlusButtonWidth;
FOUNDATION_EXTERN CGFloat CYLTabBarItemWidth;
FOUNDATION_EXTERN CGFloat CYLTabBarHeight;

@protocol CYLTabBarControllerDelegate <NSObject>
@optional
Expand Down
7 changes: 7 additions & 0 deletions CYLTabBarController/CYLTabBarController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
NSUInteger CYLTabbarItemsCount = 0;
NSUInteger CYLPlusButtonIndex = 0;
CGFloat CYLTabBarItemWidth = 0.0f;
CGFloat CYLTabBarHeight = 0.0f;

NSString *const CYLTabBarItemWidthDidChangeNotification = @"CYLTabBarItemWidthDidChangeNotification";
static void * const CYLTabImageViewDefaultOffsetContext = (void*)&CYLTabImageViewDefaultOffsetContext;

Expand Down Expand Up @@ -86,6 +88,11 @@ - (void)viewWillLayoutSubviews {
});
}

- (void)setTabBarHeight:(CGFloat)tabBarHeight {
_tabBarHeight = tabBarHeight;
CYLTabBarHeight = tabBarHeight;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
UIViewController *controller = self.selectedViewController;
if ([controller isKindOfClass:[UINavigationController class]]) {
Expand Down
4 changes: 3 additions & 1 deletion Example/MainTabBarController.m
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ - (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
// 设置背景图片
UITabBar *tabBarAppearance = [UITabBar appearance];

//FIXED: #196
//FIXED: https://github.com/ChenYilong/CYLTabBarController/issues/312
[UITabBar appearance].translucent = NO;
//FIXED: https://github.com/ChenYilong/CYLTabBarController/issues/196
NSString *tabBarBackgroundImageName = @"tabbarBg";
UIImage *tabBarBackgroundImage = [UIImage imageNamed:tabBarBackgroundImageName];
UIImage *scanedTabBarBackgroundImage = [[self class] scaleImage:tabBarBackgroundImage];
Expand Down

0 comments on commit 2ec9659

Please sign in to comment.