-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add badge label for buttons #7
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method, I think it's going to look neater. |
||
|
||
[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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -180,6 +182,17 @@ - (void)setSelectedIndex:(NSInteger)selectedIndex animated:(BOOL)animated { | |
} | ||
} | ||
|
||
- (void)setBadgeValue:(NSString*)value atIndex:(NSInteger)index | ||
{ | ||
NSString* key = [NSString stringWithFormat:@"%ld", (long)index]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For controllers and actions, I'm using
|
||
if (value) { | ||
[self.badgeValues setValue:value forKey:key]; | ||
} | ||
else { | ||
[self.badgeValues removeObjectForKey:key]; | ||
} | ||
[self updateInterfaceIfNeeded]; | ||
} | ||
|
||
#pragma mark - Actions | ||
|
||
|
@@ -206,11 +219,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 +253,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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice if we can take the badges setup to another method, say |
||
[self.buttonsContainer addSubview:badgeLabel]; | ||
self.badgeLabels[i] = badgeLabel; | ||
} | ||
|
||
[self setupButtonsConstraints]; | ||
|
@@ -259,6 +284,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 +307,16 @@ - (void)customizeButtons { | |
[button customizeForTabBarWithImage:self.tabIcons[i] | ||
selectedColor:self.selectedColor ?: [UIColor blackColor] | ||
highlighted:isHighlighted]; | ||
|
||
|
||
UILabel *badgeLabel = self.badgeLabels[i]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment for this, would be nice if we can take this logic to a |
||
NSString* badgeValue = [self.badgeValues valueForKey:[NSString stringWithFormat:@"%ld", (long)i]]; | ||
if (badgeValue) { | ||
badgeLabel.text = badgeValue; | ||
badgeLabel.hidden = NO; | ||
} | ||
else { | ||
badgeLabel.hidden = YES; | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please let's don't leave commented lines of code here :)
We could instead leave a comment that says: