Skip to content

Commit 5657b9f

Browse files
committed
v1.17.11 able to set every item titlePositionAdjustment and imageInsets
1 parent aae4aa9 commit 5657b9f

5 files changed

+80
-31
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.17.10"
3+
s.version = "1.17.11"
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/CYLTabBar.m

+15-16
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,8 @@ - (void)layoutSubviews {
142142
CYLTabBarItemWidth = (tabBarWidth) / CYLTabbarItemsCount;
143143
[self.tabBarButtonArray enumerateObjectsUsingBlock:^(UIView * _Nonnull childView, NSUInteger buttonIndex, BOOL * _Nonnull stop) {
144144
//仅修改childView的x和宽度,yh值不变
145-
childView.frame = CGRectMake(buttonIndex * CYLTabBarItemWidth,
146-
CGRectGetMinY(childView.frame),
147-
CYLTabBarItemWidth,
148-
CGRectGetHeight(childView.frame)
149-
);
145+
CGFloat childViewX = buttonIndex * CYLTabBarItemWidth;
146+
[self changeXForChildView:childView childViewX:childViewX tabBarItemWidth:CYLTabBarItemWidth];
150147
}];
151148

152149
} else {
@@ -172,11 +169,7 @@ - (void)layoutSubviews {
172169
}
173170
}
174171
//仅修改childView的x和宽度,yh值不变
175-
childView.frame = CGRectMake(childViewX,
176-
CGRectGetMinY(childView.frame),
177-
CYLTabBarItemWidth,
178-
CGRectGetHeight(childView.frame)
179-
);
172+
[self changeXForChildView:childView childViewX:childViewX tabBarItemWidth:CYLTabBarItemWidth];
180173
}];
181174
//bring the plus button to top
182175
[self bringSubviewToFront:_plusButton];
@@ -185,6 +178,15 @@ - (void)layoutSubviews {
185178
self.tabBarItemWidth = CYLTabBarItemWidth;
186179
}
187180

181+
- (void)changeXForChildView:(UIView *)childView childViewX:(CGFloat)childViewX tabBarItemWidth:(CGFloat)tabBarItemWidth {
182+
//仅修改childView的x和宽度,yh值不变
183+
childView.frame = CGRectMake(childViewX,
184+
CGRectGetMinY(childView.frame),
185+
tabBarItemWidth,
186+
CGRectGetHeight(childView.frame)
187+
);
188+
}
189+
188190
#pragma mark -
189191
#pragma mark - Private Methods
190192

@@ -267,12 +269,9 @@ - (NSUInteger)plusButtonIndex {
267269
NSUInteger plusButtonIndex;
268270
if ([[self.plusButton class] respondsToSelector:@selector(indexOfPlusButtonInTabBar)]) {
269271
plusButtonIndex = [[self.plusButton class] indexOfPlusButtonInTabBar];
270-
//仅修改self.plusButton的x,ywh值不变
271-
self.plusButton.frame = CGRectMake(plusButtonIndex * CYLTabBarItemWidth,
272-
CGRectGetMinY(self.plusButton.frame),
273-
CGRectGetWidth(self.plusButton.frame),
274-
CGRectGetHeight(self.plusButton.frame)
275-
);
272+
CGFloat childViewX = plusButtonIndex * CYLTabBarItemWidth;
273+
CGFloat tabBarItemWidth = CGRectGetWidth(self.plusButton.frame);
274+
[self changeXForChildView:self.plusButton childViewX:childViewX tabBarItemWidth:tabBarItemWidth];
276275
} else {
277276
if (CYLTabbarItemsCount % 2 != 0) {
278277
[NSException raise:NSStringFromClass([CYLTabBarController class]) format:@"If the count of CYLTabbarControllers is odd,you must realizse `+indexOfPlusButtonInTabBar` in your custom plusButton class.【Chinese】如果CYLTabbarControllers的个数是奇数,你必须在你自定义的plusButton中实现`+indexOfPlusButtonInTabBar`,来指定plusButton的位置"];

CYLTabBarController/CYLTabBarController.h

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ typedef void(^CYLViewDidLayoutSubViewsBlock)(CYLTabBarController *tabBarControll
1818
FOUNDATION_EXTERN NSString *const CYLTabBarItemTitle;
1919
FOUNDATION_EXTERN NSString *const CYLTabBarItemImage;
2020
FOUNDATION_EXTERN NSString *const CYLTabBarItemSelectedImage;
21+
FOUNDATION_EXTERN NSString *const CYLTabBarItemImageInsets;
22+
FOUNDATION_EXTERN NSString *const CYLTabBarItemTitlePositionAdjustment;
2123
FOUNDATION_EXTERN NSUInteger CYLTabbarItemsCount;
2224
FOUNDATION_EXTERN NSUInteger CYLPlusButtonIndex;
2325
FOUNDATION_EXTERN CGFloat CYLPlusButtonWidth;

CYLTabBarController/CYLTabBarController.m

+43-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
NSString *const CYLTabBarItemTitle = @"CYLTabBarItemTitle";
1515
NSString *const CYLTabBarItemImage = @"CYLTabBarItemImage";
1616
NSString *const CYLTabBarItemSelectedImage = @"CYLTabBarItemSelectedImage";
17+
NSString *const CYLTabBarItemImageInsets = @"CYLTabBarItemImageInsets";
18+
NSString *const CYLTabBarItemTitlePositionAdjustment = @"CYLTabBarItemTitlePositionAdjustment";
1719

1820
NSUInteger CYLTabbarItemsCount = 0;
1921
NSUInteger CYLPlusButtonIndex = 0;
@@ -300,18 +302,32 @@ - (void)setViewControllers:(NSArray *)viewControllers {
300302
NSString *title = nil;
301303
id normalImageInfo = nil;
302304
id selectedImageInfo = nil;
305+
UIOffset titlePositionAdjustment = UIOffsetZero;
306+
UIEdgeInsets imageInsets = UIEdgeInsetsZero;
303307
if (viewController != CYLPlusChildViewController) {
304308
title = _tabBarItemsAttributes[idx][CYLTabBarItemTitle];
305309
normalImageInfo = _tabBarItemsAttributes[idx][CYLTabBarItemImage];
306310
selectedImageInfo = _tabBarItemsAttributes[idx][CYLTabBarItemSelectedImage];
311+
312+
NSValue *offsetValue = _tabBarItemsAttributes[idx][CYLTabBarItemTitlePositionAdjustment];
313+
UIOffset offset = [offsetValue UIOffsetValue];
314+
titlePositionAdjustment = offset;
315+
316+
NSValue *insetsValue = _tabBarItemsAttributes[idx][CYLTabBarItemImageInsets];
317+
UIEdgeInsets insets = [insetsValue UIEdgeInsetsValue];
318+
imageInsets = insets;
307319
} else {
308320
idx--;
309321
}
310322

311323
[self addOneChildViewController:viewController
312324
WithTitle:title
313325
normalImageInfo:normalImageInfo
314-
selectedImageInfo:selectedImageInfo];
326+
selectedImageInfo:selectedImageInfo
327+
titlePositionAdjustment:titlePositionAdjustment
328+
imageInsets:imageInsets
329+
330+
];
315331
[[viewController cyl_getViewControllerInsteadOfNavigationController] cyl_setTabBarController:self];
316332
idx++;
317333
}
@@ -341,7 +357,10 @@ - (void)setTintColor:(UIColor *)tintColor {
341357
- (void)addOneChildViewController:(UIViewController *)viewController
342358
WithTitle:(NSString *)title
343359
normalImageInfo:(id)normalImageInfo
344-
selectedImageInfo:(id)selectedImageInfo {
360+
selectedImageInfo:(id)selectedImageInfo
361+
titlePositionAdjustment:(UIOffset)titlePositionAdjustment
362+
imageInsets:(UIEdgeInsets)imageInsets
363+
{
345364
viewController.tabBarItem.title = title;
346365
if (normalImageInfo) {
347366
UIImage *normalImage = [self getImageFromImageInfo:normalImageInfo];
@@ -351,11 +370,13 @@ - (void)addOneChildViewController:(UIViewController *)viewController
351370
UIImage *selectedImage = [self getImageFromImageInfo:selectedImageInfo];
352371
viewController.tabBarItem.selectedImage = selectedImage;
353372
}
354-
if (self.shouldCustomizeImageInsets) {
355-
viewController.tabBarItem.imageInsets = self.imageInsets;
373+
if (self.shouldCustomizeImageInsets || ([self isNOTEmptyForImageInsets:imageInsets])) {
374+
UIEdgeInsets insets = (([self isNOTEmptyForImageInsets:imageInsets]) ? imageInsets : self.imageInsets);
375+
viewController.tabBarItem.imageInsets = insets;
356376
}
357-
if (self.shouldCustomizeTitlePositionAdjustment) {
358-
viewController.tabBarItem.titlePositionAdjustment = self.titlePositionAdjustment;
377+
if (self.shouldCustomizeTitlePositionAdjustment || [self isNOTEmptyForTitlePositionAdjustment:titlePositionAdjustment]) {
378+
UIOffset offset = (([self isNOTEmptyForTitlePositionAdjustment:titlePositionAdjustment]) ? titlePositionAdjustment : self.titlePositionAdjustment);
379+
viewController.tabBarItem.titlePositionAdjustment = offset;
359380
}
360381
[self addChildViewController:viewController];
361382
}
@@ -372,15 +393,29 @@ - (UIImage *)getImageFromImageInfo:(id)imageInfo {
372393
}
373394

374395
- (BOOL)shouldCustomizeImageInsets {
375-
BOOL shouldCustomizeImageInsets = self.imageInsets.top != 0.f || self.imageInsets.left != 0.f || self.imageInsets.bottom != 0.f || self.imageInsets.right != 0.f;
396+
BOOL shouldCustomizeImageInsets = [self isNOTEmptyForImageInsets:self.imageInsets];
376397
return shouldCustomizeImageInsets;
377398
}
378399

379400
- (BOOL)shouldCustomizeTitlePositionAdjustment {
380-
BOOL shouldCustomizeTitlePositionAdjustment = self.titlePositionAdjustment.horizontal != 0.f || self.titlePositionAdjustment.vertical != 0.f;
401+
BOOL shouldCustomizeTitlePositionAdjustment = [self isNOTEmptyForTitlePositionAdjustment:self.titlePositionAdjustment];
381402
return shouldCustomizeTitlePositionAdjustment;
382403
}
383404

405+
- (BOOL)isNOTEmptyForImageInsets:(UIEdgeInsets)imageInsets {
406+
if (imageInsets.top != 0 || imageInsets.left != 0 || imageInsets.bottom != 0 || imageInsets.right != 0) {
407+
return YES;
408+
}
409+
return NO;
410+
}
411+
412+
- (BOOL)isNOTEmptyForTitlePositionAdjustment:(UIOffset)titlePositionAdjustment {
413+
if (titlePositionAdjustment.horizontal != 0 || titlePositionAdjustment.vertical != 0) {
414+
return YES;
415+
}
416+
return NO;
417+
}
418+
384419
#pragma mark -
385420
#pragma mark - KVO Method
386421

Example/CYLTabBarControllerConfig.m

+19-6
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,34 @@ - (NSArray *)tabBarItemsAttributesForController {
9797
NSDictionary *firstTabBarItemsAttributes = @{
9898
// CYLTabBarItemTitle : @"首页",
9999
CYLTabBarItemImage : @"home_normal", /* NSString and UIImage are supported*/
100-
CYLTabBarItemSelectedImage : @"home_highlight", /* NSString and UIImage are supported*/
100+
CYLTabBarItemSelectedImage : [UIImage imageNamed:@"home_highlight"], /* NSString and UIImage are supported*/
101+
// CYLTabBarItemImageInsets: [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)],
102+
// CYLTabBarItemImageInsets: [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)]
101103
};
102104
NSDictionary *secondTabBarItemsAttributes = @{
103105
// CYLTabBarItemTitle : @"同城",
104106
CYLTabBarItemImage : @"mycity_normal",
105-
CYLTabBarItemSelectedImage : @"mycity_highlight",
107+
CYLTabBarItemSelectedImage : [UIImage imageNamed:@"mycity_highlight"],
108+
// CYLTabBarItemImageInsets: [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)],
109+
// CYLTabBarItemImageInsets: [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)]
110+
111+
112+
106113
};
107114
NSDictionary *thirdTabBarItemsAttributes = @{
108115
// CYLTabBarItemTitle : @"消息",
109116
CYLTabBarItemImage : @"message_normal",
110-
CYLTabBarItemSelectedImage : @"message_highlight",
117+
CYLTabBarItemSelectedImage : [UIImage imageNamed:@"message_highlight"],
118+
// CYLTabBarItemImageInsets: [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)],
119+
// CYLTabBarItemImageInsets: [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)]
120+
111121
};
112122
NSDictionary *fourthTabBarItemsAttributes = @{
113123
// CYLTabBarItemTitle : @"我的",
114124
CYLTabBarItemImage : @"account_normal",
115-
CYLTabBarItemSelectedImage : @"account_highlight"
125+
CYLTabBarItemSelectedImage : [UIImage imageNamed:@"account_highlight"],
126+
// CYLTabBarItemImageInsets: [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)],
127+
// CYLTabBarItemImageInsets: [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)]
116128
};
117129
NSArray *tabBarItemsAttributes = @[
118130
firstTabBarItemsAttributes,
@@ -163,7 +175,7 @@ - (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
163175
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
164176
[[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
165177
// [[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"tapbar_top_line"]];
166-
178+
// [[UITabBar appearance] setTintColor:[UIColor redColor]];
167179
// set the bar background image
168180
// 设置背景图片
169181
UITabBar *tabBarAppearance = [UITabBar appearance];
@@ -207,7 +219,8 @@ - (void)customizeTabBarSelectionIndicatorImage {
207219
}
208220

209221
+ (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize {
210-
UIGraphicsBeginImageContext(CGSizeMake([UIScreen mainScreen].bounds.size.width * scaleSize, image.size.height * scaleSize));
222+
CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width * scaleSize, image.size.height * scaleSize);
223+
UIGraphicsBeginImageContextWithOptions(size, NO, 1.0);
211224
[image drawInRect:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width * scaleSize, image.size.height * scaleSize)];
212225
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
213226
UIGraphicsEndImageContext();

0 commit comments

Comments
 (0)