Skip to content

Commit d1f8cc8

Browse files
committed
v1.6.0 add API +constantOfPlusButtonCenterYOffsetForTabBarHeight:
1 parent 095f5be commit d1f8cc8

13 files changed

+136
-46
lines changed

CYLTabBarController.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "CYLTabBarController"
3-
s.version = "1.5.6"
3+
s.version = "1.6.0"
44
s.summary = "Highly customizable tabBar and tabBarController for iOS"
55
s.description = "CYLTabBarController is iPad and iPhone compatible. Supports landscape and portrait orientations and can be used inside UINavigationController."
66
s.homepage = "https://github.com/ChenYilong/CYLTabBarController"

CYLTabBarController.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
9A1559D71CB6D56500CAB1AF /* CYLSameCityViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CYLSameCityViewController.m; sourceTree = "<group>"; };
4343
9A1559E21CB6EB1F00CAB1AF /* CYLPlusButtonSubclass.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CYLPlusButtonSubclass.h; sourceTree = "<group>"; };
4444
9A1559E31CB6EB1F00CAB1AF /* CYLPlusButtonSubclass.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CYLPlusButtonSubclass.m; sourceTree = "<group>"; };
45+
9A5B29111D3475BC009E1BEE /* CYLConstants.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CYLConstants.h; sourceTree = "<group>"; };
4546
9AE667111C800085001DED12 /* UIViewController+CYLTabBarControllerExtention.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIViewController+CYLTabBarControllerExtention.h"; sourceTree = "<group>"; };
4647
9AE667121C800085001DED12 /* UIViewController+CYLTabBarControllerExtention.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+CYLTabBarControllerExtention.m"; sourceTree = "<group>"; };
4748
9AE942741BDC91B800354519 /* CYLTabBarController.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CYLTabBarController.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -201,6 +202,7 @@
201202
9AE942F41BDC937F00354519 /* CYLPlusButton.m */,
202203
9AE667111C800085001DED12 /* UIViewController+CYLTabBarControllerExtention.h */,
203204
9AE667121C800085001DED12 /* UIViewController+CYLTabBarControllerExtention.m */,
205+
9A5B29111D3475BC009E1BEE /* CYLConstants.h */,
204206
);
205207
path = CYLTabBarController;
206208
sourceTree = "<group>";

CYLTabBarController/CYLConstants.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// CYLTabBarController.h
3+
// CYLTabBarController
4+
//
5+
// Created by 微博@iOS程序犭袁 ( http://weibo.com/luohanchenyilong/ ) on 10/20/15.
6+
// Copyright © 2015 https://github.com/ChenYilong . All rights reserved.
7+
//
8+
9+
#define CYL_DEPRECATED(explain) __attribute__((deprecated(explain)))
10+

CYLTabBarController/CYLPlusButton.h

+38-13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//
88

99
@import UIKit;
10+
#import "CYLConstants.h"
11+
@class CYLPlusButton;
1012

1113
@protocol CYLPlusButtonSubclassing
1214

@@ -15,31 +17,46 @@
1517
@optional
1618

1719
/*!
18-
用来自定义加号按钮的位置,如果不实现默认居中。
19-
@attention 以下两种情况下,必须实现该协议方法,否则 CYLTabBarController 会抛出 exception 来进行提示:
20-
1. 添加了 PlusButton 且 TabBarItem 的个数是奇数。
21-
2. 实现了 `+plusChildViewController`。
22-
@return 用来自定义加号按钮在 TabBar 中的位置。
20+
* 用来自定义加号按钮的位置,如果不实现默认居中。
21+
* @attention 以下两种情况下,必须实现该协议方法,否则 CYLTabBarController 会抛出 exception 来进行提示:
22+
1. 添加了 PlusButton 且 TabBarItem 的个数是奇数。
23+
2. 实现了 `+plusChildViewController`。
24+
* @return 用来自定义加号按钮在 TabBar 中的位置。
2325
*
2426
*/
2527
+ (NSUInteger)indexOfPlusButtonInTabBar;
2628

2729
/*!
28-
该方法是为了调整自定义按钮中心点Y轴方向的位置,建议在按钮超出了 tabbar 的边界时实现该方法。
29-
30-
@return 返回值是自定义按钮中心点Y轴方向的坐标除以 tabbar 的高度,如果不实现,会自动进行比对,预设一个较为合适的位置,如果实现了该方法,预设的逻辑将失效。值如果是0.5,表示button居中,小于0.5表示button偏上,大于0.5则表示button偏下。
30+
* 该方法是为了调整 PlusButton 中心点Y轴方向的位置,建议在按钮超出了 tabbar 的边界时实现该方法。
31+
* @attention 如果不实现该方法,内部会自动进行比对,预设一个较为合适的位置,如果实现了该方法,预设的逻辑将失效。
32+
* @return 返回值是自定义按钮中心点Y轴方向的坐标除以 tabbar 的高度,
33+
内部实现时,会使用该返回值来设置 PlusButton 的 centerY 坐标,公式如下:
34+
`PlusButtonCenterY = multiplierOfTabBarHeight * taBarHeight + constantOfPlusButtonCenterYOffset;`
35+
也就是说:如果 constantOfPlusButtonCenterYOffset 为0,同时 multiplierOfTabBarHeight 的值是0.5,表示 PlusButton 居中,小于0.5表示 PlusButton 偏上,大于0.5则表示偏下。
3136
*
3237
*/
33-
+ (CGFloat)multiplerInCenterY;
38+
+ (CGFloat)multiplierOfTabBarHeight:(CGFloat)tabBarHeight;
3439

3540
/*!
36-
实现该方法后,能让 PlusButton 的点击效果与跟点击其他 UITabBarButton 效果一样,跳转到该方法指定的 UIViewController 。
37-
@attention 必须同时实现 `+indexOfPlusButtonInTabBar` 来指定 PlusButton 的位置。
38-
@return 指定 PlusButton 点击后跳转的 UIViewController。
41+
* 见 `+multiplierOfTabBarHeight:` 注释:
42+
* `PlusButtonCenterY = multiplierOfTabBarHeight * taBarHeight + constantOfPlusButtonCenterYOffset;`
43+
* 也就是说: constantOfPlusButtonCenterYOffset 大于0会向下偏移,小于0会向上偏移。
44+
* @attention 实现了该方法,但没有实现 `+multiplierOfTabBarHeight:` 方法,在这种情况下,会在预设逻辑的基础上进行偏移。
45+
*/
46+
+ (CGFloat)constantOfPlusButtonCenterYOffsetForTabBarHeight:(CGFloat)tabBarHeight;
47+
48+
/*!
49+
* 实现该方法后,能让 PlusButton 的点击效果与跟点击其他 UITabBarButton 效果一样,跳转到该方法指定的 UIViewController 。
50+
* @attention 必须同时实现 `+indexOfPlusButtonInTabBar` 来指定 PlusButton 的位置。
51+
* @return 指定 PlusButton 点击后跳转的 UIViewController。
3952
*
4053
*/
4154
+ (UIViewController *)plusChildViewController;
4255

56+
#pragma mark - Deprecated API
57+
58+
+ (CGFloat)multiplerInCenterY CYL_DEPRECATED("Deprecated in 1.6.0. Use `+multiplierOfTabBarHeight:` instead.");
59+
4360
@end
4461

4562
@class CYLTabBar;
@@ -49,8 +66,16 @@ FOUNDATION_EXTERN UIViewController *CYLPlusChildViewController;
4966

5067
@interface CYLPlusButton : UIButton
5168

52-
+ (void)registerSubclass;
69+
+ (void)registerPlusButton;
5370

5471
- (void)plusChildViewControllerButtonClicked:(UIButton<CYLPlusButtonSubclassing> *)sender;
5572

5673
@end
74+
75+
#pragma mark - Deprecated API
76+
77+
@interface CYLPlusButton (CYLDeprecated)
78+
79+
+ (void)registerSubclass CYL_DEPRECATED("Deprecated in 1.6.0. Use `+[CYLPlusButton registerPlusButton]` instead.");
80+
81+
@end

CYLTabBarController/CYLPlusButton.m

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ @implementation CYLPlusButton
1818
#pragma mark -
1919
#pragma mark - public Methods
2020

21-
+ (void)registerSubclass {
21+
+ (void)registerPlusButton {
2222
if (![self conformsToProtocol:@protocol(CYLPlusButtonSubclassing)]) {
2323
return;
2424
}
@@ -37,6 +37,13 @@ + (void)registerSubclass {
3737
}
3838
}
3939

40+
#pragma clang diagnostic push
41+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
42+
+ (void)registerSubclass {
43+
[self registerPlusButton];
44+
}
45+
#pragma clang diagnostic pop
46+
4047
- (void)plusChildViewControllerButtonClicked:(UIButton<CYLPlusButtonSubclassing> *)sender {
4148
sender.selected = YES;
4249
[self cyl_tabBarController].selectedIndex = CYLPlusButtonIndex;

CYLTabBarController/CYLTabBar.m

+31-13
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,19 @@ - (NSArray *)tabBarButtonArray {
6868

6969
- (void)layoutSubviews {
7070
[super layoutSubviews];
71-
CGFloat barWidth = self.bounds.size.width;
72-
CGFloat barHeight = self.bounds.size.height;
73-
CYLTabBarItemWidth = (barWidth - CYLPlusButtonWidth) / CYLTabbarItemsCount;
71+
CGFloat taBarWidth = self.bounds.size.width;
72+
CGFloat taBarHeight = self.bounds.size.height;
73+
CYLTabBarItemWidth = (taBarWidth - CYLPlusButtonWidth) / CYLTabbarItemsCount;
7474
self.tabBarItemWidth = CYLTabBarItemWidth;
7575
NSArray *sortedSubviews = [self sortedSubviews];
7676
self.tabBarButtonArray = [self tabBarButtonFromTabBarSubviews:sortedSubviews];
7777
[self setupSwappableImageViewDefaultOffset:self.tabBarButtonArray[0]];
7878
if (!CYLExternPlusButton) {
7979
return;
8080
}
81-
CGFloat multiplerInCenterY = [self multiplerInCenterY];
82-
self.plusButton.center = CGPointMake(barWidth * 0.5, barHeight * multiplerInCenterY);
81+
CGFloat multiplierOfTabBarHeight = [self multiplierOfTabBarHeight:taBarHeight];
82+
CGFloat constantOfPlusButtonCenterYOffset = [self constantOfPlusButtonCenterYOffsetForTabBarHeight:taBarHeight];
83+
self.plusButton.center = CGPointMake(taBarWidth * 0.5, taBarHeight * multiplierOfTabBarHeight + constantOfPlusButtonCenterYOffset);
8384
NSUInteger plusButtonIndex = [self plusButtonIndex];
8485
[self.tabBarButtonArray enumerateObjectsUsingBlock:^(UIView * _Nonnull childView, NSUInteger buttonIndex, BOOL * _Nonnull stop) {
8586
//调整UITabBarItem的位置
@@ -139,22 +140,39 @@ - (void)setSwappableImageViewDefaultOffset:(CGFloat)swappableImageViewDefaultOff
139140
}
140141
}
141142

142-
- (CGFloat)multiplerInCenterY {
143-
CGFloat multiplerInCenterY;
144-
if ([[self.plusButton class] respondsToSelector:@selector(multiplerInCenterY)]) {
145-
multiplerInCenterY = [[self.plusButton class] multiplerInCenterY];
146-
} else {
143+
- (CGFloat)multiplierOfTabBarHeight:(CGFloat)taBarHeight {
144+
CGFloat multiplierOfTabBarHeight;
145+
if ([[self.plusButton class] respondsToSelector:@selector(multiplierOfTabBarHeight:)]) {
146+
multiplierOfTabBarHeight = [[self.plusButton class] multiplierOfTabBarHeight:taBarHeight];
147+
}
148+
149+
#pragma clang diagnostic push
150+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
151+
else if ([[self.plusButton class] respondsToSelector:@selector(multiplerInCenterY)]) {
152+
multiplierOfTabBarHeight = [[self.plusButton class] multiplerInCenterY];
153+
}
154+
#pragma clang diagnostic pop
155+
156+
else {
147157
CGSize sizeOfPlusButton = self.plusButton.frame.size;
148158
CGFloat heightDifference = sizeOfPlusButton.height - self.bounds.size.height;
149159
if (heightDifference < 0) {
150-
multiplerInCenterY = 0.5;
160+
multiplierOfTabBarHeight = 0.5;
151161
} else {
152162
CGPoint center = CGPointMake(self.bounds.size.height * 0.5, self.bounds.size.height * 0.5);
153163
center.y = center.y - heightDifference * 0.5;
154-
multiplerInCenterY = center.y / self.bounds.size.height;
164+
multiplierOfTabBarHeight = center.y / self.bounds.size.height;
155165
}
156166
}
157-
return multiplerInCenterY;
167+
return multiplierOfTabBarHeight;
168+
}
169+
170+
- (CGFloat)constantOfPlusButtonCenterYOffsetForTabBarHeight:(CGFloat)taBarHeight {
171+
CGFloat constantOfPlusButtonCenterYOffset = 0.f;
172+
if ([[self.plusButton class] respondsToSelector:@selector(constantOfPlusButtonCenterYOffsetForTabBarHeight:)]) {
173+
constantOfPlusButtonCenterYOffset = [[self.plusButton class] constantOfPlusButtonCenterYOffsetForTabBarHeight:taBarHeight];
174+
}
175+
return constantOfPlusButtonCenterYOffset;
158176
}
159177

160178
- (NSUInteger)plusButtonIndex {

CYLTabBarController/CYLTabBarController.h

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
@import Foundation;
1010
@import UIKit;
11+
12+
#import "CYLPlusButton.h"
1113
#import "UIViewController+CYLTabBarControllerExtention.h"
1214

1315
FOUNDATION_EXTERN NSString *const CYLTabBarItemTitle;

CYLTabBarController/CYLTabBarController.m

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#import "CYLTabBarController.h"
1010
#import "CYLTabBar.h"
11-
#import "CYLPlusButton.h"
1211
#import <objc/runtime.h>
1312

1413
NSString *const CYLTabBarItemTitle = @"CYLTabBarItemTitle";

Example/CYLTabBarControllerConfig.m

+6-6
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,22 @@ - (NSArray *)viewControllers {
8686

8787
- (NSArray *)tabBarItemsAttributesForController {
8888
NSDictionary *firstTabBarItemsAttributes = @{
89-
CYLTabBarItemTitle : @"首页",
89+
// CYLTabBarItemTitle : @"首页",
9090
CYLTabBarItemImage : @"home_normal",
9191
CYLTabBarItemSelectedImage : @"home_highlight",
9292
};
9393
NSDictionary *secondTabBarItemsAttributes = @{
94-
CYLTabBarItemTitle : @"同城",
94+
// CYLTabBarItemTitle : @"同城",
9595
CYLTabBarItemImage : @"mycity_normal",
9696
CYLTabBarItemSelectedImage : @"mycity_highlight",
9797
};
9898
NSDictionary *thirdTabBarItemsAttributes = @{
99-
CYLTabBarItemTitle : @"消息",
99+
// CYLTabBarItemTitle : @"消息",
100100
CYLTabBarItemImage : @"message_normal",
101101
CYLTabBarItemSelectedImage : @"message_highlight",
102102
};
103103
NSDictionary *fourthTabBarItemsAttributes = @{
104-
CYLTabBarItemTitle : @"我的",
104+
// CYLTabBarItemTitle : @"我的",
105105
CYLTabBarItemImage : @"account_normal",
106106
CYLTabBarItemSelectedImage : @"account_highlight"
107107
};
@@ -121,7 +121,7 @@ - (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
121121
#warning CUSTOMIZE YOUR TABBAR APPEARANCE
122122
// Customize UITabBar height
123123
// 自定义 TabBar 高度
124-
// tabBarController.tabBarHeight = 40.f;
124+
tabBarController.tabBarHeight = 40.f;
125125

126126
// set the text color for unselected state
127127
// 普通状态下的文字属性
@@ -170,7 +170,7 @@ - (void)updateTabBarCustomizationWhenTabBarItemWidthDidUpdate {
170170
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
171171
if ((orientation == UIDeviceOrientationLandscapeLeft) || (orientation == UIDeviceOrientationLandscapeRight)) {
172172
NSLog(@"Landscape Left or Right !");
173-
} else if (orientation == UIDeviceOrientationPortrait){
173+
} else if (orientation == UIDeviceOrientationPortrait) {
174174
NSLog(@"Landscape portrait!");
175175
}
176176
[self customizeTabBarSelectionIndicatorImage];

Example/Classes/Module/Home/CYLHomeViewController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// CYLHomeViewController.m
3-
// CYLCustomTabBarDemo
3+
// CYLTabBarController
44
//
55
// Created by 微博@iOS程序犭袁 ( http://weibo.com/luohanchenyilong/ ) on 10/20/15.
66
// Copyright © 2015 https://github.com/ChenYilong . All rights reserved.

Example/Classes/Module/SameCity/CYLSameCityViewController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// CYLSameCityViewController.m
3-
// CYLCustomTabBarDemo
3+
// CYLTabBarController
44
//
55
// Created by 微博@iOS程序犭袁 ( http://weibo.com/luohanchenyilong/ ) on 10/20/15.
66
// Copyright © 2015 https://github.com/ChenYilong . All rights reserved.

Example/Classes/View/CYLPlusButtonSubclass.m

+13-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ @implementation CYLPlusButtonSubclass
2222
#pragma mark - Life Cycle
2323

2424
+ (void)load {
25-
[super registerSubclass];
25+
[super registerPlusButton];
2626
}
2727

2828
- (instancetype)initWithFrame:(CGRect)frame {
@@ -38,8 +38,10 @@ - (void)layoutSubviews {
3838
[super layoutSubviews];
3939

4040
// 控件大小,间距大小
41+
// 注意:一定要根据项目中的图片去调整下面的0.7和0.9,Demo之所以这么设置,因为demo中的 plusButton 的 icon 不是正方形。
4142
CGFloat const imageViewEdgeWidth = self.bounds.size.width * 0.7;
4243
CGFloat const imageViewEdgeHeight = imageViewEdgeWidth * 0.9;
44+
4345
CGFloat const centerOfView = self.bounds.size.width * 0.5;
4446
CGFloat const labelLineHeight = self.titleLabel.font.lineHeight;
4547
CGFloat const verticalMarginT = self.bounds.size.height - labelLineHeight - imageViewEdgeWidth;
@@ -78,6 +80,8 @@ + (id)plusButton {
7880

7981
button.titleLabel.font = [UIFont systemFontOfSize:9.5];
8082
[button sizeToFit]; // or set frame in this way `button.frame = CGRectMake(0.0, 0.0, 250, 100);`
83+
// button.frame = CGRectMake(0.0, 0.0, 250, 100);
84+
// button.backgroundColor = [UIColor redColor];
8185
[button addTarget:button action:@selector(clickPublish) forControlEvents:UIControlEventTouchUpInside];
8286
return button;
8387
}
@@ -134,13 +138,18 @@ - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSIn
134138
// initWithRootViewController:plusChildViewController];
135139
// return plusChildNavigationController;
136140
//}
137-
//
141+
142+
138143
//+ (NSUInteger)indexOfPlusButtonInTabBar {
139-
// return 2;
144+
// return 4;
140145
//}
141146

142-
+ (CGFloat)multiplerInCenterY {
147+
+ (CGFloat)multiplierOfTabBarHeight:(CGFloat)tabBarHeight {
143148
return 0.3;
144149
}
145150

151+
+ (CGFloat)constantOfPlusButtonCenterYOffsetForTabBarHeight:(CGFloat)tabBarHeight {
152+
return -10;
153+
}
154+
146155
@end

0 commit comments

Comments
 (0)