你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 一步一步完成iOS主題皮膚切換後果

一步一步完成iOS主題皮膚切換後果

編輯:IOS開發綜合

本文實例為年夜家分享了IOS主題皮膚切換代碼,供年夜家參考,詳細內容以下

1. 主題皮膚功效切換引見
主題切換就是依據用戶設置分歧的主題,來靜態轉變用戶的界面,平日會轉變navigationBar配景圖片、tabBar配景圖片、tabBar中的按鈕的圖片和選中的配景圖片、navigationItem.title 題目的字體色彩、UI中其他元素控件

下載源代碼地址: http://xiazai.jb51.net/201609/yuanma/ThemeSkinSetup(jb51.net).rar

2.項目目次構造及完成後果截圖

3. 詳細完成步調


1.將image文件夾(group)和 Skins拖入到項目工程中的資本文件夾中
2.創立BaseViewController
3.設置裝備擺設theme.plist
4.事項項目所需的根本框架供能,並完成主題的tableView功效
5.創立主題治理器:ThemeManager
6.自界說ThemeTabBarItem 控件
7.創立UI工場: UIFactory
8. 完成tableView中的didSelected事宜完成主題切換
9.記載用戶選擇的主題,以便用戶下次啟動時是前次設置的主題

1.創立BaseViewController

#import <UIKit/UIKit.h> 
@interface BaseViewController : UIViewController 
 
- (void) reloadThemeImage; 
@end 

#import "BaseViewController.h" 
 
#import "ThemeManager.h" 
#import "NotificationMacro.h" 
 
@interface BaseViewController () 
 
@end 
 
@implementation BaseViewController 
- (id) init { 
 if (self == [super init]) { 
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeChangedNotfication:) name:kThemeChangedNotification object:nil]; 
 } 
  
 [self reloadThemeImage]; 
 return self; 
} 
 
- (void)viewDidLoad { 
 [super viewDidLoad]; 
 [self reloadThemeImage]; 
} 
 
- (void)didReceiveMemoryWarning { 
 [super didReceiveMemoryWarning]; 
 // Dispose of any resources that can be recreated. 
} 
 
- (void) themeChangedNotfication:(NSNotification *)notification { 
 [self reloadThemeImage]; 
} 
 
- (void) reloadThemeImage { 
 ThemeManager * themeManager = [ThemeManager sharedThemeManager]; 
  
 UIImage * navigationBackgroundImage = [themeManager themeImageWithName:@"navigationbar_background.png"]; 
 [self.navigationController.navigationBar setBackgroundImage:navigationBackgroundImage forBarMetrics:UIBarMetricsDefault]; 
  
 UIImage * tabBarBackgroundImage = [themeManager themeImageWithName:@"tabbar_background.png"]; 
 [self.tabBarController.tabBar setBackgroundImage:tabBarBackgroundImage]; 
} 
@end 

2. 完成AppDelegate

#import "AppDelegate.h" 
 
#import "MainViewController.h" 
#import "ThemeManager.h" 
#import "NotificationMacro.h" 
 
@interface AppDelegate () 
 
@end 
 
@implementation AppDelegate 
 
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
 [self initUserDefaultConfig]; 
  
 MainViewController * rootViewController = [[MainViewController alloc] init]; 
 self.Window.rootViewController = rootViewController; 
  
 return YES; 
} 
 
 
- (void) initUserDefaultConfig { 
 NSString * themeName = [[NSUserDefaults standardUserDefaults] objectForKey:kThemeNameKey]; 
 ThemeManager * themeManager = [ThemeManager sharedThemeManager]; 
 themeManager.themeName = themeName; 
}</span></span> 

<span ><span >#import "MainViewController.h" 
 
#import "HomeViewController.h" 
#import "MessageViewController.h" 
#import "MineViewController.h" 
 
#import "UIFactory.h" 
 
 
@interface MainViewController () 
 
@end 
 
@implementation MainViewController 
 
- (id) init { 
 if (self = [super init]) { 
  [self initTabBarUI]; 
 } 
  
 return self; 
} 
 
- (void)viewDidLoad { 
 [super viewDidLoad]; 
  
} 
 
- (void)didReceiveMemoryWarning { 
 [super didReceiveMemoryWarning]; 
} 
 
 
- (void) initTabBarUI { 
 // 主頁 
 HomeViewController * homeViewController = [[HomeViewController alloc] init]; 
 UINavigationController * homeNavigationController = [[UINavigationController alloc] initWithRootViewController:homeViewController]; 
// UITabBarItem * homeTabBarItem = [[UITabBarItem alloc] initWithTitle:@"主頁" image:[UIImage imageNamed:@"tabbar_home"] selectedImage:[UIImage imageNamed:@"tabbar_home_selected"]]; 
 UITabBarItem * homeTabBarItem = [UIFactory createTabBarItemWithTitle:@"主頁" imageName:@"tabbar_home" selectedImage:@"tabbar_home_selected"]; 
 homeNavigationController.tabBarItem = homeTabBarItem; 
  
 // 新聞(中間) 
 MessageViewController * messageViewController = [[MessageViewController alloc] init]; 
 UINavigationController * messageNavigationController = [[UINavigationController alloc] initWithRootViewController:messageViewController]; 
// UITabBarItem * messageTabBarItem = [[UITabBarItem alloc] initWithTitle:@"新聞" image:[UIImage imageNamed:@"tabbar_message_center"] selectedImage:[UIImage imageNamed:@"tabbar_message_center_selected"]]; 
 UITabBarItem * messageTabBarItem = [UIFactory createTabBarItemWithTitle:@"新聞" imageName:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"]; 
 messageNavigationController.tabBarItem = messageTabBarItem; 
  
 // 我 
 MineViewController * mineViewController = [[MineViewController alloc] init]; 
 UINavigationController * mineNavigationController = [[UINavigationController alloc] initWithRootViewController:mineViewController]; 
// UITabBarItem * m.netabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"tabbar_profile"] selectedImage:[UIImage imageNamed:@"tabbar_profile_selected"]]; 
 UITabBarItem * mineTabBarItem = [UIFactory createTabBarItemWithTitle:@"我" imageName:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"]; 
  
  
 mineNavigationController.tabBarItem = mineTabBarItem; 
 NSArray * viewControllers = @[homeNavigationController, messageNavigationController, mineNavigationController]; 
 self.viewControllers = viewControllers; 
} 
 
 
@end 

3. 創立主題治理器

#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 
 
@interface ThemeManager : NSObject 
 
@property (nonatomic, copy) NSString * themeName;   // 主落款字 
@property (nonatomic, retain) NSDictionary * themePlistDict; // 主題屬性列表字典 
 
+ (ThemeManager *) sharedThemeManager; 
 
- (UIImage *) themeImageWithName:(NSString *)imageName; 
@end</span></span> 

<span ><span >#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 
 
@interface ThemeManager : NSObject 
 
@property (nonatomic, copy) NSString * themeName;   // 主落款字 
@property (nonatomic, retain) NSDictionary * themePlistDict; // 主題屬性列表字典 
 
+ (ThemeManager *) sharedThemeManager; 
 
- (UIImage *) themeImageWithName:(NSString *)imageName; 
@end 
#import "ThemeManager.h" 
#import "NotificationMacro.h" 
static ThemeManager * sharedThemeManager; 
 
@implementation ThemeManager 
 
- (id) init { 
 if(self = [super init]) { 
  NSString * themePath = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"plist"]; 
  self.themePlistDict = [NSDictionary dictionaryWithContentsOfFile:themePath]; 
  self.themeName = nil; 
 } 
  
 return self; 
} 
 
+ (ThemeManager *) sharedThemeManager { 
 @synchronized(self) { 
  if (nil == sharedThemeManager) { 
   sharedThemeManager = [[ThemeManager alloc] init]; 
  } 
 } 
  
 return sharedThemeManager; 
} 
 
// Override 重寫themeName的set辦法 
- (void) setThemeName:(NSString *)themeName { 
 _themeName = themeName; 
} 
 
- (UIImage *) themeImageWithName:(NSString *)imageName { 
 if (imageName == nil) { 
  return nil; 
 } 
  
 NSString * themePath = [self themePath]; 
 NSString * themeImagePath = [themePath stringByAppendingPathComponent:imageName]; 
 UIImage * themeImage = [UIImage imageWithContentsOfFile:themeImagePath]; 
  
 return themeImage; 
} 
 
// 前往主題途徑 
- (NSString *)themePath { 
 NSString * resourcePath = [[NSBundle mainBundle] resourcePath]; 
 if (self.themeName == nil || [self.themeName isEqualToString:@""]) { 
  return resourcePath; 
 } 
  
  
 NSString * themeSubPath = [self.themePlistDict objectForKey:self.themeName]; // Skins/blue 
 NSString * themeFilePath = [resourcePath stringByAppendingPathComponent:themeSubPath]; // .../Skins/blue 
  
 return themeFilePath; 
} 
@end 

4. 創立主題按鈕 ThemeTabBarItem

#import <UIKit/UIKit.h> 
@interface ThemeTabBarItem : UITabBarItem 
 
@property (nonatomic, copy) NSString * imageName; 
@property (nonatomic, copy) NSString * selectedImageName; 
 
 
- (id) initWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName; 
 
@end </span></span> 

<span ><span >#import "ThemeTabBarItem.h" 
#import "ThemeManager.h" 
#import "NotificationMacro.h" 
 
@implementation ThemeTabBarItem 
 
// 初始化時注冊不雅察者 
- (id) init { 
 if (self = [super init]) { 
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeChangedNotification:) name:kThemeChangedNotification object:nil]; 
 } 
  
 return self; 
} 
 
- (id) initWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName { 
 if (self = [self init]) { 
  self.title = title; 
  self.imageName = imageName;   // 此時會挪用[self setImageName:imageName] ---> [self reloadThemeImage] --->[self setImage:image] 
  self.selectedImageName = selectedImageName;// 此時會挪用[self setSelectedImageName:selectedImageName]; 
 } 
  
 return self; 
} 
 
 
#pragma mark - 
#pragma mark - Override Setter 
- (void) setImageName:(NSString *)imageName { 
 if (_imageName != imageName) { 
  _imageName = imageName; 
 } 
  
 [self reloadThemeImage]; 
} 
 
- (void) setSelectedImageName:(NSString *)selectedImageName { 
 if (_selectedImageName != selectedImageName) { 
  _selectedImageName = selectedImageName; 
 } 
  
 [self reloadThemeImage]; 
} 
 
 
 
// 主題轉變以後從新加載圖片 
- (void)themeChangedNotification:(NSNotification *)notification { 
 [self reloadThemeImage]; 
} 
 
- (void)reloadThemeImage { 
 ThemeManager * themeManager = [ThemeManager sharedThemeManager]; 
  
 if (self.imageName != nil) { 
  UIImage * image = [themeManager themeImageWithName:self.imageName]; 
  [self setImage:image]; 
 } 
  
 if (self.selectedImageName != nil) { 
  UIImage * selectedImage = [themeManager themeImageWithName:self.selectedImageName]; 
  [self setSelectedImage:selectedImage]; 
 } 
} 
 
- (void) dealloc { 
 [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

5. 創立UI工場

#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 
 
@interface UIFactory : NSObject 
 
+ (UITabBarItem *) createTabBarItemWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName; 
 
 
@end</span></span> 
<span ><span >#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 
 
@interface UIFactory : NSObject 
 
+ (UITabBarItem *) createTabBarItemWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName; 
 
 
@end 
#import "UIFactory.h" 
 
#import "ThemeTabBarItem.h" 
@implementation UIFactory 
 
+ (UITabBarItem *) createTabBarItemWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName { 
 ThemeTabBarItem * themeTabBarItem = [[ThemeTabBarItem alloc] initWithTitle:title imageName:imageName selectedImage:selectedImageName]; 
  
 return themeTabBarItem; 
} 
@end 

6. 完成選中單位格的事宜

#import "BaseViewController.h" 
 
@interface MineViewController : BaseViewController <UITableViewDelegate, UITableViewDataSource> 
 
 
@property (weak, nonatomic) IBOutlet UITableView *tableView; 
 
@property (nonatomic, retain) NSMutableArray * themeDataSource; 
@end 

#import "BaseViewController.h" 
 
@interface MineViewController : BaseViewController <UITableViewDelegate, UITableViewDataSource> 
 
 
@property (weak, nonatomic) IBOutlet UITableView *tableView; 
 
@property (nonatomic, retain) NSMutableArray * themeDataSource; 
@end 
#import "MineViewController.h" 
 
#import "ThemeManager.h" 
#import "NotificationMacro.h" 
 
@interface MineViewController () 
 
@end 
 
@implementation MineViewController 
 
- (void)viewDidLoad { 
 [super viewDidLoad]; 
 self.title = @"我"; 
  
 ThemeManager * themeManager = [ThemeManager sharedThemeManager]; 
 _themeDataSource = [NSMutableArray arrayWithArray:themeManager.themePlistDict.allKeys]; 
} 
 
- (void)didReceiveMemoryWarning { 
 [super didReceiveMemoryWarning]; 
 // Dispose of any resources that can be recreated. 
} 
 
 
 
#pragma mark - 
#pragma mark - UITableViewDelegate 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
 
 return self.themeDataSource.count; 
} 
 
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
 static NSString * Identifier = @"Cell"; 
 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:Identifier]; 
 if (cell == nil) { 
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]; 
 } 
  
 NSString * text = self.themeDataSource[indexPath.row]; 
 cell.textLabel.text = text; 
  
 ThemeManager * themeManager = [ThemeManager sharedThemeManager]; 
 NSString * currentTheme = themeManager.themeName; 
 if (currentTheme == nil) { 
  currentTheme = @"默許"; 
 } 
 if ([currentTheme isEqualToString:text]) { 
  cell.AccessoryType = UITableViewCellAccessoryCheckmark; 
 } else { 
  cell.AccessoryType = UITableViewCellAccessoryNone; 
 } 
  
 return cell; 
} 
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
  
 ThemeManager * themeManager = [ThemeManager sharedThemeManager]; 
 NSString * themeName = self.themeDataSource[indexPath.row]; 
  
 if ([themeName isEqualToString:@"默許"]) { 
  themeName = nil; 
 } 
  
 // 記載以後主落款字 
 themeManager.themeName = themeName; 
 [[NSNotificationCenter defaultCenter] postNotificationName:kThemeChangedNotification object:nil]; 
  
  
 // 主題耐久化 
 NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; 
 [userDefaults setObject:themeName forKey:kThemeNameKey]; 
 [userDefaults synchronize]; 
  
 // 從新加載數據顯示UITableViewCellAccessoryCheckmark 顯示選中的對號 v 
 [self.tableView reloadData]; 
}

以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐本站。

【一步一步完成iOS主題皮膚切換後果】的相關資料介紹到這裡,希望對您有所幫助!提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved