1 // 覆蓋setHighlighted,取消點擊時的高亮狀態2 - (void)setHighlighted:(BOOL)highlighted {3 // [super setHighlighted:highlighted];4 }
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設置frame26 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添加到tabBar33 [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 }
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 @end16 17 @implementation HVWTabBarController18 19 - (void)viewDidLoad {20 [super viewDidLoad];21 // Do any additional setup after loading the view.22 23 // 1.刪除原來的TabBar24 [self.tabBar removeFromSuperview];25 26 // 2.添加自定義TabBar27 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 @optional16 - (void) hvwTabBar:(HVWTabBar *) hvwTabBar didClickedButtonFrom:(int) from to:(int) to;17 @end18 19 @interface HVWTabBar : UIView20 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 513 14 @interface HVWTabBar()15 16 @property(nonatomic, weak) HVWTabBarButton *selectedButton;17 18 @end19 20 @implementation HVWTabBar21 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添加到tabBar44 [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
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 }
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.設置tabBar11 [self.tabBar addSubview:hvwTabBar];12 }
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 @end15 16 @implementation HVWNavigationController17 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中的tabBar54 [super pushViewController:viewController animated:YES];55 }56 57 @end58
新聞熱點
疑難解答