wxxsw / SwiftTheme

🎨 Powerful theme/skin manager for iOS 9+ 主题/换肤, 暗色模式
MIT License
2.52k stars 305 forks source link

How to dynamically change UITabbarItem images using plist demo ? #26

Closed RathaKrishna closed 7 years ago

RathaKrishna commented 7 years ago

Is there is any way to use plist demo on OC project to change UITabbarItem's image and selectedImage dynamically based on theme ?

wxxsw commented 7 years ago

Because UITabBarItem can not dynamically modify the image, so you need to create a new item when updating the theme.

In the following steps:

First:

Register the theme update in the controller that sets the tabBarItem, or in the custom UITabBarController subclass.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTabBarItem) name:@"ThemeUpdateNotification" object:nil];

Second:

How to set plist, can be found in the document and PlistDemo. And then set the image to be updated, and then set it to tabBarItem is complete.

- (void)updateTabBarItem {

    UIImage *image = [ThemeManager imageForKeyPath:@"some_image_keypath"];
    UIImage *selectedImage = [ThemeManager imageForKeyPath:@"some_selected_image_keypath"];

    // If it is in the subclass of UITabBarController, update the corresponding controller one by one.
    self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Some Title" image:image selectedImage:selectedImage];
}

Please tell me if it is resolved.

RathaKrishna commented 7 years ago

Yes it's working like charm !!! thank you.