简易的主题切换功能

这个思路来自于我的一个朋友。
代码如下:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

/*
 *切换主题颜色使用
 */
@property (nonatomic,assign) BOOL isNightMode;

@end

#import "UIColor+Theme.h"
#import "AppDelegate.h"
@implementation UIColor (Theme)

+ (UIColor *)navigationBarColor
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (appDelegate.isNightMode == NO)
    {
        return [UIColor magentaColor];
    }
    return [UIColor darkTextColor];
}
@end
- (IBAction)changThemeColor:(id)sender
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UISwitch *swit = (UISwitch *)sender;
    if ([swit isOn])
    {
        appDelegate.isNightMode = YES;
    }
    else
    {
        appDelegate.isNightMode = NO;
    }
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor navigationBarColor]];
}

我们在AppDelegate中创建一个全局的BOOL属性变量isNightMode,然后创建一个UIColor的类目,在这里进行颜色切换管理。当我们改变isNightMode的值时,自然也会改变对应颜色的值。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容