Skip to content

Commit 97b77dc

Browse files
committedDec 5, 2017
v1.6.0 support add red point on iPhoneX
1 parent 78f6496 commit 97b77dc

6 files changed

+38
-52
lines changed
 

‎CYLTabBarController/CYLConstants.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
#define CYL_DEPRECATED(explain) __attribute__((deprecated(explain)))
1313
#define CYL_IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
14-
#define CYL_IS_IPHONE_X (CYL_IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 812.0f)
15-
#define CYL_IS_IOS_11 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.f)
1614

15+
//[[UIScreen mainScreen] bounds].size.height == 812.0f
16+
//[[UIScreen mainScreen] bounds].size.width
17+
#define CYL_IS_IOS_11 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.f)
18+
#define CYL_IS_IPHONE_X (CYL_IS_IOS_11 && CYL_IS_IPHONE && (MIN([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) == 375 && MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) == 812))
1719
#endif /* CYLConstants_h */
1820

1921

‎CYLTabBarController/CYLPlusButton.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232
* @attention 如果不实现该方法,内部会自动进行比对,预设一个较为合适的位置,如果实现了该方法,预设的逻辑将失效。
3333
* @return 返回值是自定义按钮中心点Y轴方向的坐标除以 tabbar 的高度,
3434
内部实现时,会使用该返回值来设置 PlusButton 的 centerY 坐标,公式如下:
35-
`PlusButtonCenterY = multiplierOfTabBarHeight * taBarHeight + constantOfPlusButtonCenterYOffset;`
35+
`PlusButtonCenterY = multiplierOfTabBarHeight * tabBarHeight + constantOfPlusButtonCenterYOffset;`
3636
也就是说:如果 constantOfPlusButtonCenterYOffset 为0,同时 multiplierOfTabBarHeight 的值是0.5,表示 PlusButton 居中,小于0.5表示 PlusButton 偏上,大于0.5则表示偏下。
3737
*
3838
*/
3939
+ (CGFloat)multiplierOfTabBarHeight:(CGFloat)tabBarHeight;
4040

4141
/*!
4242
* 见 `+multiplierOfTabBarHeight:` 注释:
43-
* `PlusButtonCenterY = multiplierOfTabBarHeight * taBarHeight + constantOfPlusButtonCenterYOffset;`
43+
* `PlusButtonCenterY = multiplierOfTabBarHeight * tabBarHeight + constantOfPlusButtonCenterYOffset;`
4444
* 也就是说: constantOfPlusButtonCenterYOffset 大于0会向下偏移,小于0会向上偏移。
4545
* @attention 实现了该方法,但没有实现 `+multiplierOfTabBarHeight:` 方法,在这种情况下,会在预设逻辑的基础上进行偏移。
4646
*/

‎CYLTabBarController/CYLTabBar.m

+16-23
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,12 @@ - (instancetype)sharedInit {
5757
- (CGSize)sizeThatFits:(CGSize)size {
5858
CGSize sizeThatFits = [super sizeThatFits:size];
5959
CGFloat height = [self cyl_tabBarController].tabBarHeight;
60-
if (height > 0 && !CYL_IS_IPHONE_X && CYL_IS_IOS_11) {
60+
if (height > 0) {
6161
sizeThatFits.height = [self cyl_tabBarController].tabBarHeight;
6262
}
6363
return sizeThatFits;
6464
}
6565

66-
- (void)setFrame:(CGRect)frame {
67-
if (CYL_IS_IPHONE_X) {
68-
if (self.superview && CGRectGetMaxY(self.superview.bounds) != CGRectGetMaxY(frame)) {
69-
frame.origin.y = CGRectGetHeight(self.superview.bounds) - CGRectGetHeight(frame);
70-
}
71-
}
72-
[super setFrame:frame];
73-
}
74-
7566
/**
7667
* lazy load tabBarButtonArray
7768
*
@@ -86,19 +77,19 @@ - (NSArray *)tabBarButtonArray {
8677

8778
- (void)layoutSubviews {
8879
[super layoutSubviews];
89-
CGFloat taBarWidth = self.bounds.size.width;
90-
CGFloat taBarHeight = self.bounds.size.height;
91-
CYLTabBarItemWidth = (taBarWidth - CYLPlusButtonWidth) / CYLTabbarItemsCount;
80+
CGFloat tabBarWidth = self.bounds.size.width;
81+
CGFloat tabBarHeight = self.bounds.size.height;
82+
CYLTabBarItemWidth = (tabBarWidth - CYLPlusButtonWidth) / CYLTabbarItemsCount;
9283
self.tabBarItemWidth = CYLTabBarItemWidth;
9384
NSArray *sortedSubviews = [self sortedSubviews];
9485
self.tabBarButtonArray = [self tabBarButtonFromTabBarSubviews:sortedSubviews];
9586
[self setupTabImageViewDefaultOffset:self.tabBarButtonArray[0]];
9687
if (!CYLExternPlusButton) {
9788
return;
9889
}
99-
CGFloat multiplierOfTabBarHeight = [self multiplierOfTabBarHeight:taBarHeight];
100-
CGFloat constantOfPlusButtonCenterYOffset = [self constantOfPlusButtonCenterYOffsetForTabBarHeight:taBarHeight];
101-
self.plusButton.center = CGPointMake(taBarWidth * 0.5, taBarHeight * multiplierOfTabBarHeight + constantOfPlusButtonCenterYOffset);
90+
CGFloat multiplierOfTabBarHeight = [self multiplierOfTabBarHeight:tabBarHeight];
91+
CGFloat constantOfPlusButtonCenterYOffset = [self constantOfPlusButtonCenterYOffsetForTabBarHeight:tabBarHeight];
92+
self.plusButton.center = CGPointMake(tabBarWidth * 0.5, tabBarHeight * multiplierOfTabBarHeight + constantOfPlusButtonCenterYOffset);
10293
NSUInteger plusButtonIndex = [self plusButtonIndex];
10394
[self.tabBarButtonArray enumerateObjectsUsingBlock:^(UIView * _Nonnull childView, NSUInteger buttonIndex, BOOL * _Nonnull stop) {
10495
//调整UITabBarItem的位置
@@ -134,9 +125,13 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
134125
}
135126
if(context == CYLTabBarContext) {
136127
[[NSNotificationCenter defaultCenter] postNotificationName:CYLTabBarItemWidthDidChangeNotification object:self];
128+
if (CYL_IS_IPHONE_X && CYL_IS_IOS_11) {
129+
[self layoutIfNeeded];
130+
}
137131
}
138132
}
139133

134+
140135
- (void)dealloc {
141136
// KVO反注册
142137
[self removeObserver:self forKeyPath:@"tabBarItemWidth"];
@@ -158,10 +153,10 @@ - (void)setTabImageViewDefaultOffset:(CGFloat)tabImageViewDefaultOffset {
158153
}
159154
}
160155

161-
- (CGFloat)multiplierOfTabBarHeight:(CGFloat)taBarHeight {
156+
- (CGFloat)multiplierOfTabBarHeight:(CGFloat)tabBarHeight {
162157
CGFloat multiplierOfTabBarHeight;
163158
if ([[self.plusButton class] respondsToSelector:@selector(multiplierOfTabBarHeight:)]) {
164-
multiplierOfTabBarHeight = [[self.plusButton class] multiplierOfTabBarHeight:taBarHeight];
159+
multiplierOfTabBarHeight = [[self.plusButton class] multiplierOfTabBarHeight:tabBarHeight];
165160
}
166161

167162
#pragma clang diagnostic push
@@ -185,10 +180,10 @@ - (CGFloat)multiplierOfTabBarHeight:(CGFloat)taBarHeight {
185180
return multiplierOfTabBarHeight;
186181
}
187182

188-
- (CGFloat)constantOfPlusButtonCenterYOffsetForTabBarHeight:(CGFloat)taBarHeight {
183+
- (CGFloat)constantOfPlusButtonCenterYOffsetForTabBarHeight:(CGFloat)tabBarHeight {
189184
CGFloat constantOfPlusButtonCenterYOffset = 0.f;
190185
if ([[self.plusButton class] respondsToSelector:@selector(constantOfPlusButtonCenterYOffsetForTabBarHeight:)]) {
191-
constantOfPlusButtonCenterYOffset = [[self.plusButton class] constantOfPlusButtonCenterYOffsetForTabBarHeight:taBarHeight];
186+
constantOfPlusButtonCenterYOffset = [[self.plusButton class] constantOfPlusButtonCenterYOffsetForTabBarHeight:tabBarHeight];
192187
}
193188
return constantOfPlusButtonCenterYOffset;
194189
}
@@ -257,7 +252,7 @@ - (void)setupTabImageViewDefaultOffset:(UIView *)tabBarButton {
257252
shouldCustomizeImageView = NO;
258253
}
259254
}];
260-
if (shouldCustomizeImageView && !CYL_IS_IPHONE_X) {
255+
if (shouldCustomizeImageView) {
261256
self.tabImageViewDefaultOffset = tabImageViewDefaultOffset;
262257
}
263258
}
@@ -267,7 +262,6 @@ - (void)setupTabImageViewDefaultOffset:(UIView *)tabBarButton {
267262
*/
268263
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
269264
//1. 边界情况:不能响应点击事件
270-
271265
BOOL canNotResponseEvent = self.hidden || (self.alpha <= 0.01f) || (self.userInteractionEnabled == NO);
272266
if (canNotResponseEvent) {
273267
return nil;
@@ -301,7 +295,6 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
301295
}
302296

303297
//3. 最后处理 TabBarItems 凸出的部分、添加到 TabBar 上的自定义视图、点击到 TabBar 上的空白区域
304-
305298
UIView *result = [super hitTest:point withEvent:event];
306299
if (result) {
307300
return result;

‎CYLTabBarController/CYLTabBarController.m

+13-15
Original file line numberDiff line numberDiff line change
@@ -35,45 +35,43 @@ @implementation CYLTabBarController
3535

3636
- (void)viewDidLoad {
3737
[super viewDidLoad];
38+
if (CYL_IS_IPHONE_X) {
39+
self.tabBarHeight = 83;
40+
}
3841
// 处理tabBar,使用自定义 tabBar 添加 发布按钮
3942
[self setUpTabBar];
4043
// KVO注册监听
4144
if (!self.isObservingTabImageViewDefaultOffset) {
4245
[self.tabBar addObserver:self forKeyPath:@"tabImageViewDefaultOffset" options:NSKeyValueObservingOptionNew context:CYLTabImageViewDefaultOffsetContext];
4346
self.observingTabImageViewDefaultOffset = YES;
4447
}
45-
4648
}
4749

4850
- (void)setViewDidLayoutSubViewsBlock:(CYLViewDidLayoutSubViewsBlock)viewDidLayoutSubviewsBlock {
4951
_viewDidLayoutSubviewsBlock = viewDidLayoutSubviewsBlock;
5052
}
5153

52-
//Fix issue #93
5354
- (void)viewDidLayoutSubviews {
54-
[self.tabBar layoutSubviews];
55-
static dispatch_once_t onceToken;
56-
dispatch_once(&onceToken, ^{
57-
UITabBar *tabBar = self.tabBar;
58-
for (UIControl *control in tabBar.subviews) {
59-
if ([control isKindOfClass:[UIControl class]]) {
60-
SEL actin = @selector(didSelectControl:);
61-
[control addTarget:self action:actin forControlEvents:UIControlEventTouchUpInside];
62-
}
55+
[self.tabBar layoutSubviews];//Fix issue #93
56+
UITabBar *tabBar = self.tabBar;
57+
for (UIControl *control in tabBar.subviews) {
58+
if ([control isKindOfClass:[UIControl class]]) {
59+
SEL actin = @selector(didSelectControl:);
60+
[control addTarget:self action:actin forControlEvents:UIControlEventTouchUpInside];
6361
}
64-
!self.viewDidLayoutSubviewsBlock ?: self.viewDidLayoutSubviewsBlock(self);
65-
});
62+
}
63+
!self.viewDidLayoutSubviewsBlock ?: self.viewDidLayoutSubviewsBlock(self);
6664
}
6765

6866
- (void)viewWillLayoutSubviews {
69-
if (CYL_IS_IOS_11 || !self.tabBarHeight) {
67+
if (!(self.tabBarHeight > 0)) {
7068
return;
7169
}
7270
self.tabBar.frame = ({
7371
CGRect frame = self.tabBar.frame;
7472
CGFloat tabBarHeight = self.tabBarHeight;
7573
frame.size.height = tabBarHeight;
76-
frame.origin.y = self.view.frame.size.height - tabBarHeight;
74+
frame.origin.y = [UIScreen mainScreen].bounds.size.height - tabBarHeight;
7775
frame;
7876
});
7977
}

‎CYLTabBarController/UIControl+CYLTabBarControllerExtention.m

-6
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,10 @@ - (void)cyl_removeTabBadgePoint {
2222
}
2323

2424
- (BOOL)cyl_isShowTabBadgePoint {
25-
if (CYL_IS_IPHONE_X) {
26-
return NO;
27-
}
2825
return !self.cyl_tabBadgePointView.hidden;
2926
}
3027

3128
- (void)cyl_setShowTabBadgePointIfNeeded:(BOOL)showTabBadgePoint {
32-
if (CYL_IS_IPHONE_X) {
33-
return;
34-
}
3529
@try {
3630
[self cyl_setShowTabBadgePoint:showTabBadgePoint];
3731
} @catch (NSException *exception) {

‎Example/CYLTabBarControllerConfig.m

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
// Copyright © 2015 https://github.com/ChenYilong . All rights reserved.
77
//
88
#import "CYLTabBarControllerConfig.h"
9+
#import <UIKit/UIKit.h>
10+
911
static CGFloat const CYLTabBarControllerHeight = 40.f;
1012

1113
@interface CYLBaseNavigationController : UINavigationController
1214
@end
1315

1416
@implementation CYLBaseNavigationController
1517

16-
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
17-
#define IS_IPHONE_X (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 812.0f)
18-
1918
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
2019
if (self.viewControllers.count > 0) {
2120
viewController.hidesBottomBarWhenPushed = YES;
@@ -129,7 +128,7 @@ - (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
129128
#warning CUSTOMIZE YOUR TABBAR APPEARANCE
130129
// Customize UITabBar height
131130
// 自定义 TabBar 高度
132-
// tabBarController.tabBarHeight = CYLTabBarControllerHeight;
131+
tabBarController.tabBarHeight = CYL_IS_IPHONE_X ? 65 : 40;
133132

134133
// set the text color for unselected state
135134
// 普通状态下的文字属性

0 commit comments

Comments
 (0)