A.需求
1.模仿“網易彩票”做出有5個導航頁面和相應功能的Demo
2.v1.0 版本搭建基本框架
code source:https://github.com/hellovoidworld/HelloLottery
B.搭建基本框架
1.拖入TaBarController,5個NavigationController和對應的5個UIViewController
2.配置圖標和啟動畫面
AppIcon直接拖入圖片
LaunchImage在Xcode6中需要先更改啟動圖使用圖庫的圖片,而不是LaunchImage.xib
2.引入圖片包
4. 按照模塊分類代碼包
3.底部導航--自定義TabBar
(1)基本設計
a.自定義HVWTabBarController、繼承自UIView的HVWTabBar、繼承UIButton的HVWTabBarButton
b.在自定義TabBar控制器viewDidLoad,刪除原來的TabBar,加上自定義的TabBar
c.加入自定義的按鈕圖片、選中圖片
d.選中事件,轉換圖片
e.按下馬上激發按鈕激發
重寫TabBarButton的[button setHighlighted:]取消高亮狀態
HVWTabBarButton:
1 // 覆蓋setHighlighted,取消點擊時的高亮狀態 2 - (void)setHighlighted:(BOOL)highlighted { 3 // [super setHighlighted:highlighted]; 4 }
初步實現都在HVWTabBarController的viewDidLoad方法中:
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 // Do any additional setup after loading the view. 4 5 // 1.刪除原來的TabBar 6 [self.tabBar removeFromSuperview]; 7 8 // 2.添加自定義TabBar 9 HVWTabBar *hvwTabBar = [[HVWTabBar alloc] init]; 10 hvwTabBar.frame = self.tabBar.frame; 11 hvwTabBar.backgroundColor = [UIColor greenColor]; // 設置綠色底的tabBar,稍后會被按鈕圖片覆蓋 12 [self.view addSubview:hvwTabBar]; 13 14 // 3.添加按鈕 15 for (int i=0; i<5; i++) { 16 // 3.1創建按鈕 17 HVWTabBarButton *button = [HVWTabBarButton buttonWithType:UIButtonTypeCustom]; 18 button.tag = i; 19 20 // 3.2設置按鈕背景圖片 21 [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%d", i+1]] forState:UIControlStateNormal]; 22 23 [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%dSel", i+1]] forState:UIControlStateSelected]; 24 25 // 3.3設置frame 26 CGFloat buttonWidth = hvwTabBar.frame.size.width / 5; 27 CGFloat buttonHeight = hvwTabBar.frame.size.height; 28 CGFloat buttonX = i * buttonWidth; 29 CGFloat buttonY = 0; 30 button.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight); 31 32 // 3.4添加到tabBar 33 [hvwTabBar addSubview:button]; 34 35 // 3.5添加監聽事件 36 [button addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 37 38 // 3.6默認已經點擊了第一個按鈕 39 if (i == 0) { 40 [self tabBarButtonClicked:button]; 41 } 42 } 43 } 44 45 - (void) tabBarButtonClicked:(HVWTabBarButton *) button { 46 // 1.取消選中之前的按鈕 47 self.selectedButton.selected = NO; 48 49 // 2.選中新點擊的按鈕 50 button.selected = YES; 51 52 // 3.設置為當前選中的按鈕 53 self.selectedButton = button; 54 55 // 4.切換子控制器 56 self.selectedIndex = button.tag; 57 }
(2)封裝TabBar代碼
a.重寫initWithFrame:創建初始化TabBar
b.移動子控件的初始化代碼到layoutSubviews
c.通過代理轉換Navigation頁面
d.封裝添加按鈕函數
1 // 2 // HWTabBarController.m 3 // HelloLottery 4 // 5 // Created by hellovoidworld on 14/12/31. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "HvWTabBarController.h" 10 #import "HVWTabBar.h" 11 #import "HVWTabBarButton.h" 12 13 @interface HVWTabBarController () <HVWTabBarDelegate> 14 15 @end 16 17 @implementation HVWTabBarController 18 19 - (void)viewDidLoad { 20 [super viewDidLoad]; 21 // Do any additional setup after loading the view. 22 23 // 1.刪除原來的TabBar 24 [self.tabBar removeFromSuperview]; 25 26 // 2.添加自定義TabBar 27 HVWTabBar *hvwTabBar = [[HVWTabBar alloc] init]; 28 hvwTabBar.frame = self.tabBar.frame; 29 hvwTabBar.backgroundColor = [UIColor greenColor]; // 設置綠色底的tabBar,稍后會被按鈕圖片覆蓋 30 hvwTabBar.delegate = self; 31 [self.view addSubview:hvwTabBar]; 32 } 33 34 - (void)didReceiveMemoryWarning { 35 [super didReceiveMemoryWarning]; 36 // Dispose of any resources that can be recreated. 37 } 38 39 #pragma mark - HVWTabBarDelegate 代理方法 40 - (void)hvwTabBar:(HVWTabBar *)hvwTabBar didClickedButtonFrom:(int)from to:(int)to { 41 // 切換子控制器 42 self.selectedIndex = to; 43 } 44 45 @end
1 // 2 // HVWTabBar.h 3 // HelloLottery 4 // 5 // Created by hellovoidworld on 14/12/31. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @class HVWTabBar; 12 13 /** 代理協議 */ 14 @protocol HVWTabBarDelegate <NSObject> 15 @optional 16 - (void) hvwTabBar:(HVWTabBar *) hvwTabBar didClickedButtonFrom:(int) from to:(int) to; 17 @end 18 19 @interface HVWTabBar : UIView 20 21 /** 代理 */ 22 @property(nonatomic, weak) id<HVWTabBarDelegate> delegate; 23 24 @end
1 // 2 // HVWTabBar.m 3 // HelloLottery 4 // 5 // Created by hellovoidworld on 14/12/31. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "HVWTabBar.h" 10 #import "HVWTabBarButton.h" 11 12 #define HVWTabBarButtonCount 5 13 14 @interface HVWTabBar() 15 16 @property(nonatomic, weak) HVWTabBarButton *selectedButton; 17 18 @end 19 20 @implementation HVWTabBar 21 22 // 重寫initWithFrame方法,添加tabBar按鈕 23 - (instancetype)initWithFrame:(CGRect)frame { 24 if (self = [super initWithFrame:frame]) { 25 [self initButtons]; 26 } 27 28 return self; 29 } 30 31 /** 初始化按鈕 */ 32 - (void) initButtons { 33 for (int i=0; i<HVWTabBarButtonCount; i++) { 34 // 3.1創建按鈕 35 HVWTabBarButton *button = [HVWTabBarButton buttonWithType:UIButtonTypeCustom]; 36 button.tag = i; 37 38 // 3.2設置按鈕背景圖片 39 [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%d", i+1]] forState:UIControlStateNormal]; 40 41 [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%dSel", i+1]] forState:UIControlStateSelected]; 42 43 // 3.3添加到tabBar 44 [self addSubview:button]; 45 46 // 3.4添加監聽事件 47 [button addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 48 49 // 3.5默認已經點擊了第一個按鈕 50 if (i == 0) { 51 [self tabBarButtonClicked:button]; 52 } 53 } 54 } 55 56 /** 初始化子控件的位置尺寸 */ 57 - (void)layoutSubviews { 58 [super layoutSubviews]; 59 60 for (int i=0; i<HVWTabBarButtonCount; i++) { 61 HVWTabBarButton *button = self.subviews[i]; 62 CGFloat buttonWidth = self.frame.size.width / 5; 63 CGFloat buttonHeight = self.frame.size.height; 64 CGFloat buttonX = i * buttonWidth; 65 CGFloat buttonY = 0; 66 button.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight); 67 } 68 } 69 70 - (void) tabBarButtonClicked:(HVWTabBarButton *) button { 71 // 1.調用代理方法,通知TabBarController切換子控制器 72 if ([self.delegate respondsToSelector:@selector(hvwTabBar:didClickedButtonFrom:to:)]) { 73 [self.delegate hvwTabBar:self didClickedButtonFrom:self.selectedButton.tag to:button.tag]; 74 } 75 76 // 2.取消選中之前的按鈕 77 self.selectedButton.selected = NO; 78 79 // 3.選中新點擊的按鈕 80 button.selected = YES; 81 82 // 4.設置為當前選中的按鈕 83 self.selectedButton = button; 84 } 85 86 @end
4.頭部Navigation導航欄主題設置
(1)讓applicatoin管理狀態欄
(2)Navigation導航欄背景圖片
(3)統一設置所有Navigation導航欄
[UINavigationBar appearance]; // 所有Navigation導航欄(頭部導航欄)
(4)設置所有Navigation導航欄字體顏色
(5)根據系統版本,設置Navigation導航欄背景圖片
(6)在二級頁面隱藏底部導航條,重寫導航控制器的push方法
a.自定義一個導航控制器HVWNavigationController類,重寫push,隱藏底部導航欄
b.設置為每個NavigationController的class
c.導航控制器類的initialize只會在類第一次使用的時候調用一次
所以,導航欄的主題設置可以放在initialize方法中
解決:
(1)使用application管理狀態欄
設置
不使用控制器控制狀態欄
在AppDelegate中設置:
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // Override point for customization after application launch. 3 4 // 設置狀態欄樣式為白色 5 application.statusBarStyle = UIStatusBarStyleLightContent; 6 7 return YES; 8 }
(2)創建自定義NavigationController類,並設置5個Navigation控制器的class為此類
(3)不要刪除原來的tabBar,而是覆蓋它,才能控制push事件中底部導航欄
HVWTabBarController:
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 // Do any additional setup after loading the view. 4 5 // 1.添加自定義TabBar 6 HVWTabBar *hvwTabBar = [[HVWTabBar alloc] init]; 7 hvwTabBar.frame = self.tabBar.bounds; 8 hvwTabBar.delegate = self; 9 10 // 2.設置tabBar 11 [self.tabBar addSubview:hvwTabBar]; 12 }
(4)在NavigationController中編寫類初始化方法和重寫push方法
1 // 2 // HVWNavigationController.m 3 // HelloLottery 4 // 5 // Created by hellovoidworld on 15/1/1. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "HVWNavigationController.h" 10 11 12 @interface HVWNavigationController () 13 14 @end 15 16 @implementation HVWNavigationController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 // Do any additional setup after loading the view. 21 22 } 23 24 - (void)didReceiveMemoryWarning { 25 [super didReceiveMemoryWarning]; 26 // Dispose of any resources that can be recreated. 27 } 28 29 /** 類初始化方法,僅調用一次 */ 30 + (void) initialize { 31 // 獲取能夠控制所有NavigationBar的實例 32 UINavigationBar *navBar = [UINavigationBar appearance]; 33 34 // 設置背景圖片 35 NSString *bgImageName; 36 if (iOS7) { // 在HelloLottery-Prefix.pch中定義了判斷iOS版本的全局變量 37 bgImageName = @"NavBar64"; 38 } else { 39 bgImageName = @"NavBar"; 40 } 41 42 [navBar setBackgroundImage:[UIImage imageNamed:bgImageName] forBarMetrics:UIBarMetricsDefault]; 43 44 // 設置文本 45 NSMutableDictionary *attr = [NSMutableDictionary dictionary]; 46 attr[NSForegroundColorAttributeName] = [UIColor whiteColor]; 47 attr[NSFontAttributeName] = [UIFont systemFontOfSize:16]; 48 [navBar setTitleTextAttributes:attr]; 49 } 50 51 // 攔截所有的push操作 52 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { 53 viewController.hidesBottomBarWhenPushed = YES; // 這是原來NavigationController中的tabBar,所以要設置自定義的tabBar為Navigation中的tabBar 54 [super pushViewController:viewController animated:YES]; 55 } 56 57 @end 58