diff --git a/Example/Controllers/ViewController.m b/Example/Controllers/ViewController.m index 27cec98..ac7ec8b 100644 --- a/Example/Controllers/ViewController.m +++ b/Example/Controllers/ViewController.m @@ -77,6 +77,12 @@ - (void)viewDidLoad { [weakSelf showAlertViewWithTitle:@"Globe" message:@"You have chosen the globe view controller."]; } atIndex:4]; + +// tabBarController.badgeFont = [UIFont systemFontOfSize:10 weight:2]; +// tabBarController.badgeOffset = 10; +// tabBarController.badgeSize = 20; +// tabBarController.badgeTextColor = [UIColor colorWithHexString:@"#F6EBE0"]; + [tabBarController setBadgeValue:@"99" atIndex:0]; } diff --git a/Source/Categories/ESTabBarController+Autolayout.m b/Source/Categories/ESTabBarController+Autolayout.m old mode 100644 new mode 100755 index 007f19e..7df76e7 --- a/Source/Categories/ESTabBarController+Autolayout.m +++ b/Source/Categories/ESTabBarController+Autolayout.m @@ -14,6 +14,7 @@ @interface ESTabBarController () @property (nonatomic, weak) UIView *controllersContainer; @property (nonatomic, weak) UIView *buttonsContainer; @property (nonatomic, strong) NSMutableArray *buttons; +@property (nonatomic, strong) NSMutableArray *badgeLabels; @property (nonatomic, assign) NSArray *tabIcons; @property (nonatomic, strong) UIView *selectionIndicator; @property (nonatomic, strong) NSLayoutConstraint *selectionIndicatorLeadingConstraint; @@ -36,10 +37,16 @@ - (void)setupButtonsConstraints { [self.view addConstraints:[self verticalLayoutConstraintsForButtonAtIndex:i]]; [self.view addConstraint:[self widthLayoutConstraintForButtonAtIndex:i]]; [self.view addConstraint:[self heightLayoutConstraintForButtonAtIndex:i]]; + + [self.badgeLabels[i] setTranslatesAutoresizingMaskIntoConstraints:NO]; + + [self.view addConstraint:[self horizontalLayoutConstraintsForBadgeLabelAtIndex:i]]; + [self.view addConstraint:[self verticalLayoutConstraintsForBadgeLabelAtIndex:i]]; + [self.view addConstraint:[self widthLayoutConstraintForBadgeLabelAtIndex:i]]; + [self.view addConstraint:[self heightLayoutConstraintForBadgeLabelAtIndex:i]]; } } - - (void)setupSelectionIndicatorConstraints { self.selectionIndicatorLeadingConstraint = [self leadingLayoutConstraintForIndicator]; @@ -168,5 +175,55 @@ - (NSArray *)bottomLayoutConstraintsForIndicator { views:@{@"selectionIndicator": self.selectionIndicator}]; } +- (NSLayoutConstraint *)widthLayoutConstraintForBadgeLabelAtIndex:(NSInteger)index { + UILabel *label = self.badgeLabels[index]; + + return [NSLayoutConstraint constraintWithItem:label + attribute:NSLayoutAttributeWidth + relatedBy:NSLayoutRelationEqual + toItem:nil + attribute:NSLayoutAttributeWidth + multiplier:1.0f + constant:self.badgeSize]; +} + + +- (NSLayoutConstraint *)heightLayoutConstraintForBadgeLabelAtIndex:(NSInteger)index { + UILabel *label = self.badgeLabels[index]; + + return [NSLayoutConstraint constraintWithItem:label + attribute:NSLayoutAttributeHeight + relatedBy:NSLayoutRelationEqual + toItem:nil + attribute:NSLayoutAttributeHeight + multiplier:1.0f + constant:self.badgeSize]; +} + +- (NSLayoutConstraint *)verticalLayoutConstraintsForBadgeLabelAtIndex:(NSInteger)index { + UILabel *label = self.badgeLabels[index]; + UIButton *button = self.buttons[index]; + + return [NSLayoutConstraint constraintWithItem:label + attribute:NSLayoutAttributeCenterY + relatedBy:NSLayoutRelationEqual + toItem:button + attribute:NSLayoutAttributeCenterY + multiplier:1.0 + constant:-self.badgeOffset]; +} + +- (NSLayoutConstraint *)horizontalLayoutConstraintsForBadgeLabelAtIndex:(NSInteger)index { + UILabel *label = self.badgeLabels[index]; + UIButton *button = self.buttons[index]; + + return [NSLayoutConstraint constraintWithItem:label + attribute:NSLayoutAttributeCenterX + relatedBy:NSLayoutRelationEqual + toItem:button + attribute:NSLayoutAttributeCenterX + multiplier:1.0 + constant:self.badgeOffset]; +} @end diff --git a/Source/Controllers/ESTabBarController.h b/Source/Controllers/ESTabBarController.h old mode 100644 new mode 100755 index 2668f0f..9cc7dff --- a/Source/Controllers/ESTabBarController.h +++ b/Source/Controllers/ESTabBarController.h @@ -33,6 +33,25 @@ typedef void (^ESTabBarAction)(void); // ones a bit transparent. @property (nonatomic, assign) BOOL highlightsSelectedButton; +// The current view controller stack. +@property (nonatomic, copy, readonly) NSArray *viewControllers; + +// Color to use for badge text +// Defaults to [UIColor whiteColor] +@property (nonatomic, strong) UIColor *badgeTextColor; + +// Font for badge text +// Defaults to [UIFont systemFontOfSize:8] +@property (nonatomic, strong) UIFont *badgeFont; + +// Badge width and height in pt +// Defaults to 16 +@property (nonatomic, assign) NSInteger badgeSize; + +// Badge offset from button's center (up and right) +// Defaults to 12 +@property (nonatomic, assign) NSInteger badgeOffset; + /** Initializes the tab bar with an array of UIImage that will be the icons @@ -88,5 +107,10 @@ typedef void (^ESTabBarAction)(void); */ - (void)setSelectedIndex:(NSInteger)selectedIndex animated:(BOOL)animated; +/** + Sets the badge text value. + Set nil to remove badge. + */ +- (void)setBadgeValue:(NSString*)value atIndex:(NSInteger)index; @end diff --git a/Source/Controllers/ESTabBarController.m b/Source/Controllers/ESTabBarController.m old mode 100644 new mode 100755 index e8f903d..c1d6669 --- a/Source/Controllers/ESTabBarController.m +++ b/Source/Controllers/ESTabBarController.m @@ -25,6 +25,8 @@ @interface ESTabBarController () @property (nonatomic, assign) BOOL didSetupInterface; @property (nonatomic, strong) NSMutableArray *buttons; @property (nonatomic, strong) NSMutableSet *highlightedButtonIndexes; +@property (nonatomic, strong) NSMutableDictionary *badgeValues; +@property (nonatomic, strong) NSMutableArray *badgeLabels; @property (nonatomic, strong) NSArray *tabIcons; @property (nonatomic, strong) UIView *selectionIndicator; @property (nonatomic, strong) NSLayoutConstraint *selectionIndicatorLeadingConstraint; @@ -95,6 +97,18 @@ - (void)setSeparatorLineColor:(UIColor *)color { self.separatorLine.backgroundColor = color; } +-(NSArray *)viewControllers +{ + NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithCapacity:self.tabIcons.count]; + for (NSInteger index = 0; index < self.tabIcons.count; index++) { + UIViewController* controller = self.controllers[@(index)]; + if (controller) { + [viewControllers addObject:controller]; + } + } + return viewControllers; +} + #pragma mark - UIViewController @@ -180,6 +194,17 @@ - (void)setSelectedIndex:(NSInteger)selectedIndex animated:(BOOL)animated { } } +- (void)setBadgeValue:(NSString*)value atIndex:(NSInteger)index +{ + NSString* key = [NSString stringWithFormat:@"%ld", (long)index]; + if (value) { + [self.badgeValues setValue:value forKey:key]; + } + else { + [self.badgeValues removeObjectForKey:key]; + } + [self updateInterfaceIfNeeded]; +} #pragma mark - Actions @@ -206,11 +231,18 @@ - (void)initializeWithTabIcons:(NSArray *)tabIcons { self.actions = [NSMutableDictionary dictionaryWithCapacity:tabIcons.count]; self.highlightedButtonIndexes = [NSMutableSet set]; + self.badgeValues = [NSMutableDictionary dictionaryWithCapacity:tabIcons.count]; // No selected index at first. _selectedIndex = -1; self.separatorLineColor = [UIColor lightGrayColor]; + + self.badgeTextColor = [UIColor whiteColor]; + self.badgeFont = [UIFont systemFontOfSize:8]; + self.badgeSize = 16; + self.badgeOffset = 12; + } @@ -233,12 +265,17 @@ - (void)setupInterface { - (void)setupButtons { if (self.buttons == nil) { self.buttons = [NSMutableArray arrayWithCapacity:self.tabIcons.count]; + self.badgeLabels = [NSMutableArray arrayWithCapacity:self.tabIcons.count]; for (NSInteger i = 0; i < self.tabIcons.count; i++) { UIButton *button = [self createButtonForIndex:i]; [self.buttonsContainer addSubview:button]; self.buttons[i] = button; + + UILabel *badgeLabel = [self createBadgeLabel]; + [self.buttonsContainer addSubview:badgeLabel]; + self.badgeLabels[i] = badgeLabel; } [self setupButtonsConstraints]; @@ -259,6 +296,20 @@ - (UIButton *)createButtonForIndex:(NSInteger)index { return button; } +- (UILabel*)createBadgeLabel +{ + UILabel *label = [UILabel new]; + + label.font = self.badgeFont; + label.textAlignment = NSTextAlignmentCenter; + label.backgroundColor = self.selectedColor; + label.textColor = self.badgeTextColor ?: [UIColor whiteColor]; + label.layer.cornerRadius = self.badgeSize / 2; + label.layer.masksToBounds = YES; + + return label; +} + - (void)customizeButtons { for (NSInteger i = 0; i < self.tabIcons.count; i++) { @@ -268,7 +319,16 @@ - (void)customizeButtons { [button customizeForTabBarWithImage:self.tabIcons[i] selectedColor:self.selectedColor ?: [UIColor blackColor] highlighted:isHighlighted]; - + + UILabel *badgeLabel = self.badgeLabels[i]; + NSString* badgeValue = [self.badgeValues valueForKey:[NSString stringWithFormat:@"%ld", (long)i]]; + if (badgeValue) { + badgeLabel.text = badgeValue; + badgeLabel.hidden = NO; + } + else { + badgeLabel.hidden = YES; + } } } @@ -372,5 +432,4 @@ - (void)moveSelectionIndicatorToIndex:(NSInteger)index animated:(BOOL)animated { } } - @end