From ec6fbf624e27ac4f7c798bf7d93966ff74a930d7 Mon Sep 17 00:00:00 2001 From: Vijay Vikram Singh Date: Mon, 2 Dec 2019 16:01:01 -0800 Subject: [PATCH] fix(ios): tintColor not working for TabbedBar in iOS 13 (#11329) * fix(ios): tintColor not working for TabbedBar in iOS 13 * fix(ios): added doc --- apidoc/Titanium/UI/ButtonBar.yml | 25 ++++++++++ iphone/Classes/TiUIButtonBar.m | 79 +++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/apidoc/Titanium/UI/ButtonBar.yml b/apidoc/Titanium/UI/ButtonBar.yml index 241b0005032..0a87a96e593 100644 --- a/apidoc/Titanium/UI/ButtonBar.yml +++ b/apidoc/Titanium/UI/ButtonBar.yml @@ -76,6 +76,31 @@ properties: type: [Array, Array] + - 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 . + 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 . + 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 . + type: String + platforms: [iphone, ipad] + since: "9.0.0" + osver: {ios: {min: "13.0"}} + examples: - title: Simple 3 button button bar example: | diff --git a/iphone/Classes/TiUIButtonBar.m b/iphone/Classes/TiUIButtonBar.m index ffa87856dbf..effc1fa3ba7 100644 --- a/iphone/Classes/TiUIButtonBar.m +++ b/iphone/Classes/TiUIButtonBar.m @@ -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! @@ -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];