Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Add night/multiple themes for UITabBarItem. #89

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
37A0AF191EBDAC5700CE059B /* DKAttribute.m in Sources */ = {isa = PBXBuildFile; fileRef = 37A0AF181EBDAC5700CE059B /* DKAttribute.m */; };
37A0AF1C1EBDAC6300CE059B /* UITabBarItem+Night.m in Sources */ = {isa = PBXBuildFile; fileRef = 37A0AF1B1EBDAC6300CE059B /* UITabBarItem+Night.m */; };
7261ADF91CB9FB2D007E283C /* PresentingViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7261ADF21CB9FB2D007E283C /* PresentingViewController.m */; };
7261ADFA1CB9FB2D007E283C /* RootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7261ADF41CB9FB2D007E283C /* RootViewController.m */; };
7261ADFB1CB9FB2D007E283C /* SuccViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7261ADF61CB9FB2D007E283C /* SuccViewController.m */; };
Expand All @@ -22,6 +24,10 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
37A0AF171EBDAC5700CE059B /* DKAttribute.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DKAttribute.h; sourceTree = "<group>"; };
37A0AF181EBDAC5700CE059B /* DKAttribute.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DKAttribute.m; sourceTree = "<group>"; };
37A0AF1A1EBDAC6300CE059B /* UITabBarItem+Night.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITabBarItem+Night.h"; sourceTree = "<group>"; };
37A0AF1B1EBDAC6300CE059B /* UITabBarItem+Night.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITabBarItem+Night.m"; sourceTree = "<group>"; };
7261ADF11CB9FB2D007E283C /* PresentingViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PresentingViewController.h; sourceTree = "<group>"; };
7261ADF21CB9FB2D007E283C /* PresentingViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PresentingViewController.m; sourceTree = "<group>"; };
7261ADF31CB9FB2D007E283C /* RootViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RootViewController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -92,6 +98,10 @@
729324041CB9FAF1009E544E /* Assets.xcassets */,
729324061CB9FAF1009E544E /* LaunchScreen.storyboard */,
729324091CB9FAF1009E544E /* Info.plist */,
37A0AF171EBDAC5700CE059B /* DKAttribute.h */,
37A0AF181EBDAC5700CE059B /* DKAttribute.m */,
37A0AF1A1EBDAC6300CE059B /* UITabBarItem+Night.h */,
37A0AF1B1EBDAC6300CE059B /* UITabBarItem+Night.m */,
729323F81CB9FAF1009E544E /* Supporting Files */,
);
path = Example;
Expand Down Expand Up @@ -180,6 +190,8 @@
7261ADFC1CB9FB2D007E283C /* TableViewCell.m in Sources */,
729324001CB9FAF1009E544E /* ViewController.m in Sources */,
729323FD1CB9FAF1009E544E /* AppDelegate.m in Sources */,
37A0AF1C1EBDAC6300CE059B /* UITabBarItem+Night.m in Sources */,
37A0AF191EBDAC5700CE059B /* DKAttribute.m in Sources */,
729323FA1CB9FAF1009E544E /* main.m in Sources */,
7261ADF91CB9FB2D007E283C /* PresentingViewController.m in Sources */,
7261ADFB1CB9FB2D007E283C /* SuccViewController.m in Sources */,
Expand Down
28 changes: 24 additions & 4 deletions Example/Example/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#import "AppDelegate.h"
#import "RootViewController.h"
#import "SuccViewController.h"
#import "ViewController.h"

#import "UITabBarItem+Night.h"

@interface AppDelegate ()

Expand All @@ -18,13 +21,30 @@ @implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

RootViewController *vc1 = [[RootViewController alloc] init];
vc1.tabBarItem.title = @"Root";
[vc1.tabBarItem dk_setImage:DKImagePickerWithNames(@"tabBar_association_normal", @"tabBar_profile_normal") forState:(UIControlStateNormal)];
[vc1.tabBarItem dk_setImage:DKImagePickerWithNames(@"tabBar_association_selected", @"tabBar_profile_selected") forState:(UIControlStateSelected)];
[vc1.tabBarItem dk_setTitleAttributePicker:DKAttributePickerWithAttributes(@{NSForegroundColorAttributeName: [UIColor blackColor]}, @{NSForegroundColorAttributeName: [UIColor orangeColor]}, @{NSForegroundColorAttributeName: [UIColor grayColor]}) forState:(UIControlStateNormal)];
[vc1.tabBarItem dk_setTitleAttributePicker:DKAttributePickerWithAttributes(@{NSForegroundColorAttributeName: [UIColor redColor]}, @{NSForegroundColorAttributeName: [UIColor blueColor]}, @{NSForegroundColorAttributeName: [UIColor greenColor]}) forState:(UIControlStateSelected)];
UINavigationController *navigation1 = [[UINavigationController alloc] initWithRootViewController:vc1];

ViewController *vc2 = [[ViewController alloc] init];
vc2.tabBarItem.title = @"My";
[vc2.tabBarItem dk_setImage:DKImagePickerWithNames(@"tabBar_profile_normal", @"tabBar_association_normal") forState:(UIControlStateNormal)];
[vc2.tabBarItem dk_setImage:DKImagePickerWithNames(@"tabBar_profile_selected", @"tabBar_association_selected") forState:(UIControlStateSelected)];
[vc2.tabBarItem dk_setTitleAttributePicker:DKAttributePickerWithAttributes(@{NSForegroundColorAttributeName: [UIColor blackColor]}, @{NSForegroundColorAttributeName: [UIColor orangeColor]}, @{NSForegroundColorAttributeName: [UIColor grayColor]}) forState:(UIControlStateNormal)];
[vc2.tabBarItem dk_setTitleAttributePicker:DKAttributePickerWithAttributes(@{NSForegroundColorAttributeName: [UIColor redColor]}, @{NSForegroundColorAttributeName: [UIColor blueColor]}, @{NSForegroundColorAttributeName: [UIColor greenColor]}) forState:(UIControlStateSelected)];
UINavigationController *navigation2 = [[UINavigationController alloc] initWithRootViewController:vc2];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
UITabBarController *tabController = [[UITabBarController alloc] init];
tabController.viewControllers = @[navigation1, navigation2];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
self.window.rootViewController = navigation;
UISearchBar *searchBar = [[UISearchBar alloc] init];

return YES;
}

Expand Down
10 changes: 10 additions & 0 deletions Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions Example/Example/DKAttribute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// DKAttribute.h
// ESports
//
// Created by 时彬强 on 2017/5/5.
// Copyright © 2017年 QQLS. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef NSString DKThemeVersion;

typedef NSDictionary *(^DKAttributePicker)(DKThemeVersion *themeVersion);

DKAttributePicker DKAttributePickerWithAttributes(NSDictionary *normal, ...);

@interface DKAttribute : NSObject

@end
41 changes: 41 additions & 0 deletions Example/Example/DKAttribute.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// DKAttribute.m
// ESports
//
// Created by 时彬强 on 2017/5/5.
// Copyright © 2017年 QQLS. All rights reserved.
//

#import "DKAttribute.h"

#import <DKNightVersion/DKNightVersion.h>
//#import "DKNightVersionManager.h"
//#import "DKColorTable.h"

@implementation DKAttribute

DKAttributePicker DKAttributePickerWithAttributes(NSDictionary *normal, ...) {

NSArray<DKThemeVersion *> *themes = [DKColorTable sharedColorTable].themes;
NSMutableArray<NSDictionary *> *attributes = [[NSMutableArray alloc] initWithCapacity:themes.count];
[attributes addObject:normal];
NSUInteger num_args = themes.count - 1;
va_list colors_list;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wvarargs"
va_start(colors_list, num_args);
#pragma clang diagnostic pop

for (NSUInteger i = 0; i < num_args; i++) {
NSDictionary *attribute = va_arg(colors_list, NSDictionary *);
[attributes addObject:attribute];
}
va_end(colors_list);

return ^(DKThemeVersion *themeVersion) {
NSUInteger index = [themes indexOfObject:themeVersion];
return attributes[index];
};
}

@end
20 changes: 20 additions & 0 deletions Example/Example/UITabBarItem+Night.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// UITabBarItem+Night.h
// ESports
//
// Created by 时彬强 on 2017/5/5.
// Copyright © 2017年 QQLS. All rights reserved.
//

#import <UIKit/UIKit.h>

#import "DKAttribute.h"
#import <DKNightVersion/DKNightVersion.h>
//#import "NSObject+Night.h"

@interface UITabBarItem (Night)

- (void)dk_setTitleAttributePicker:(DKAttributePicker)picker forState:(UIControlState)state;
- (void)dk_setImage:(DKImagePicker)picker forState:(UIControlState)state;

@end
91 changes: 91 additions & 0 deletions Example/Example/UITabBarItem+Night.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// UITabBarItem+Night.m
// ESports
//
// Created by 时彬强 on 2017/5/5.
// Copyright © 2017年 QQLS. All rights reserved.
//

#import "UITabBarItem+Night.h"

@interface UITabBarItem ()

@property (nonatomic, strong) NSMutableDictionary<NSString *, id> *pickers;

@end

@implementation UITabBarItem (Night)

- (void)dk_setTitleAttributePicker:(DKAttributePicker)picker forState:(UIControlState)state {
[self setTitleTextAttributes:picker(self.dk_manager.themeVersion) forState:state];

NSString *key = [NSString stringWithFormat:@"%@", @(state)];
NSMutableDictionary *dictionary = [self.pickers valueForKey:key];
if (!dictionary) {
dictionary = [[NSMutableDictionary alloc] init];
}
[dictionary setValue:[picker copy] forKey:NSStringFromSelector(@selector(setTitleTextAttributes:forState:))];
[self.pickers setValue:dictionary forKey:key];
}

- (void)dk_setImage:(DKImagePicker)picker forState:(UIControlState)state {

if (UIControlStateNormal == state) {
[self setImage:picker(self.dk_manager.themeVersion)];
} else if (UIControlStateSelected == state) {
[self setSelectedImage:picker(self.dk_manager.themeVersion)];
}

NSString *key = [NSString stringWithFormat:@"%@", @(state)];
NSMutableDictionary *dictionary = [self.pickers valueForKey:key];
if (!dictionary) {
dictionary = [[NSMutableDictionary alloc] init];
}

if (UIControlStateNormal == state) {
[dictionary setValue:[picker copy] forKey:NSStringFromSelector(@selector(setImage:))];
} else if (UIControlStateSelected == state) {
[dictionary setValue:[picker copy] forKey:NSStringFromSelector(@selector(setSelectedImage:))];
}
[self.pickers setValue:dictionary forKey:key];
}

- (void)night_updateColor {

[self.pickers enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[NSDictionary class]]) {
NSDictionary<NSString *, DKAttributePicker> *dictionary = (NSDictionary *)obj;
[dictionary enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull selector, DKAttributePicker _Nonnull picker, BOOL * _Nonnull stop) {
UIControlState state = [key integerValue];
if ([selector isEqualToString:NSStringFromSelector(@selector(setTitleTextAttributes:forState:))]) {
NSDictionary *attribute = picker(self.dk_manager.themeVersion);
[self setTitleTextAttributes:attribute forState:state];
} else if ([selector isEqualToString:NSStringFromSelector(@selector(setImage:))]) {
if (UIControlStateNormal == state) {
UIImage *resultImage = ((DKImagePicker)picker)(self.dk_manager.themeVersion);
[self setImage:resultImage];
}
} else if ([selector isEqualToString:NSStringFromSelector(@selector(setSelectedImage:))]) {
if (UIControlStateSelected == state) {
UIImage *resultImage = ((DKImagePicker)picker)(self.dk_manager.themeVersion);
[self setSelectedImage:resultImage];
}
}
}];
} else {
SEL sel = NSSelectorFromString(key);
DKAttributePicker picker = (DKAttributePicker)obj;
NSDictionary *attribute = picker(self.dk_manager.themeVersion);
[UIView animateWithDuration:DKNightVersionAnimationDuration
animations:^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector:sel withObject:attribute];
#pragma clang diagnostic pop
}];

}
}];
}

@end