Skip to content

Commit

Permalink
fix(ios): tintColor not working for TabbedBar in iOS 13 (#11329)
Browse files Browse the repository at this point in the history
* fix(ios): tintColor not working for TabbedBar in  iOS 13

* fix(ios): added doc
  • Loading branch information
vijaysingh-axway authored and lokeshchdhry committed Dec 3, 2019
1 parent 495d76c commit ec6fbf6
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
25 changes: 25 additions & 0 deletions apidoc/Titanium/UI/ButtonBar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ properties:
</Alloy>
type: [Array<String>, Array<BarItemType>]

- name: textColor
summary: Color of title of button, as a color name or hex triplet.
description: |
For information about color values, see the "Colors" section of <Titanium.UI>.
type: String
platforms: [iphone, ipad]
since: "9.0.0"

- name: selectedTextColor
summary: Color of title of button when it is selected, as a color name or hex triplet.
description: |
For information about color values, see the "Colors" section of <Titanium.UI>.
type: String
platforms: [iphone, ipad]
since: "9.0.0"

- name: selectedButtonColor
summary: Color of selected button, as a color name or hex triplet.
description: |
For information about color values, see the "Colors" section of <Titanium.UI>.
type: String
platforms: [iphone, ipad]
since: "9.0.0"
osver: {ios: {min: "13.0"}}

examples:
- title: Simple 3 button button bar
example: |
Expand Down
79 changes: 78 additions & 1 deletion iphone/Classes/TiUIButtonBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,42 @@ - (id)accessibilityElement
return [self segmentedControl];
}

- (UIColor *)reverseColorOf:(UIColor *)oldColor
{
CGColorRef oldCGColor = oldColor.CGColor;

int numberOfComponents = CGColorGetNumberOfComponents(oldCGColor);
// can not invert - the only component is the alpha
if (numberOfComponents == 1) {
return [UIColor colorWithCGColor:oldCGColor];
}

const CGFloat *oldComponentColors = CGColorGetComponents(oldCGColor);
CGFloat newComponentColors[numberOfComponents];

int i = numberOfComponents - 1;
newComponentColors[i] = oldComponentColors[i]; // alpha
while (--i >= 0) {
newComponentColors[i] = 1 - oldComponentColors[i];
}

CGColorRef newCGColor = CGColorCreate(CGColorGetColorSpace(oldCGColor), newComponentColors);
UIColor *newColor = [UIColor colorWithCGColor:newCGColor];
CGColorRelease(newCGColor);

//For the GRAY colors 'Middle level colors'
CGFloat white = 0;
[oldColor getWhite:&white alpha:nil];

if (white > 0.3 && white < 0.67) {
if (white >= 0.5)
newColor = [UIColor darkGrayColor];
else if (white < 0.5)
newColor = [UIColor blackColor];
}
return newColor;
}

// For regression #1880. Because there are essentially TWO kinds of 'width' going on with tabbed/button bars
// (width of all elements, width of the proxy) we assume that if the user has set the width of the bar completely,
// AND the width of the proxy is undefined, they want magic!
Expand All @@ -84,12 +120,53 @@ - (void)setTabbedBar:(BOOL)newIsTabbed;
[[self segmentedControl] setMomentary:!newIsTabbed];
}

- (void)setTintColor_:(id)value
{
UIColor *color = [[TiUtils colorValue:value] color];

if ([TiUtils isIOSVersionLower:@"13.0"]) {
[[self segmentedControl] setTintColor:color];
return;
}

UIColor *newColor = [self reverseColorOf:color];
[[self segmentedControl] setTitleTextAttributes:@{ NSForegroundColorAttributeName : color } forState:UIControlStateNormal];
[[self segmentedControl] setTitleTextAttributes:@{ NSForegroundColorAttributeName : newColor } forState:UIControlStateSelected];

[[self segmentedControl] setSelectedSegmentTintColor:color];
}

- (void)setBackgroundColor_:(id)value
{
TiColor *color = [TiUtils colorValue:value];
[[self segmentedControl] setTintColor:[color _color]];
[[self segmentedControl] setBackgroundColor:[color _color]];
}

- (void)setTextColor_:(id)value
{
UIColor *color = [[TiUtils colorValue:value] color];

[[self segmentedControl] setTitleTextAttributes:@{ NSForegroundColorAttributeName : color } forState:UIControlStateNormal];
}

- (void)setSelectedTextColor_:(id)value
{
UIColor *color = [[TiUtils colorValue:value] color];

[[self segmentedControl] setTitleTextAttributes:@{ NSForegroundColorAttributeName : color } forState:UIControlStateSelected];
}

#if IS_SDK_IOS_13
- (void)setSelectedButtonColor_:(id)value
{
if (![TiUtils isIOSVersionOrGreater:@"13.0"]) {
return;
}
UIColor *color = [[TiUtils colorValue:value] color];
[[self segmentedControl] setSelectedSegmentTintColor:color];
}
#endif

- (void)setIndex_:(id)value
{
selectedIndex = [TiUtils intValue:value def:-1];
Expand Down

0 comments on commit ec6fbf6

Please sign in to comment.