先來(lái)看一下效果圖:
1.支持導(dǎo)航欄顏色自定義
2.支持返回按鈕自定義
3.支持導(dǎo)航欄右側(cè)按鈕自定義
4.支持導(dǎo)航欄標(biāo)題自定義
5.支持所有頁(yè)面手勢(shì)側(cè)滑
6.支持tabbar item 文字顏色自定義(選中狀態(tài)和非選中狀態(tài))
- 1.導(dǎo)入原資源,如tabbar的items圖片等
- 2.創(chuàng)建.pch文件
在項(xiàng)目->Bulid Settings->搜prefix header->在后面加入你的.pch文件的絕對(duì)路徑
(一個(gè)簡(jiǎn)單方法就是點(diǎn)出prefix header后面的框后,把工程目錄里面的.pch文件拖到框里,就自動(dòng)生成它的絕對(duì)路徑了)
- 3.導(dǎo)入需要使用的三方庫(kù)MLNavigationController,這個(gè)控制器繼承自UINavigationController,加入了返回手勢(shì)
也可以用系統(tǒng)的導(dǎo)航欄,系統(tǒng)的導(dǎo)航欄現(xiàn)在也自帶返回滑動(dòng)手勢(shì)
- 4.創(chuàng)建繼承于UITabBarController的tabbar控制器
- 5.AppDelegate中初始化
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[SJTabBarViewController alloc] init];;
[self.window makeKeyAndVisible];
-
6.創(chuàng)建基類(lèi)控制器 如:SJBaseViewController。在基類(lèi)控制器里面主要設(shè)置導(dǎo)航欄及其一些屬性
~6.1 基類(lèi) .h文件
~6.2 基類(lèi) .m文件
//設(shè)置導(dǎo)航欄顏色
- (void)setNavBarStyle
{
self.titleViewLB.textColor = color_whiteColor;
self.navigationController.navigationBar.barTintColor = kNavigationBarColor;
self.navigationController.navigationBar.translucent = NO;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
UINavigationBar *navigationBar = self.navigationController.navigationBar;
[navigationBar setBackgroundImage:[UIImage imageNamed:@"account_headBg"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[navigationBar setShadowImage:[UIImage new]];
}
//baseBack方法
- (void)baseBack
{
[self.navigationController popViewControllerAnimated:YES];
}
//setter and getter方法
- (UILabel *)titleViewLB
{
if (_titleViewLB == nil) {
_titleViewLB = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth-140, 44)];
_titleViewLB.backgroundColor = [UIColor clearColor];
_titleViewLB.textColor = color_whiteColor
_titleViewLB.font = XSBlodFont(19);
_titleViewLB.textAlignment = NSTextAlignmentCenter;
}
return _titleViewLB;
}
- (void)setNavTitle:(NSString *)navTitle{
_titleViewLB.text = navTitle;
}
- (void)setNavTitleColor:(UIColor *)navTitleColor{
_titleViewLB.textColor = navTitleColor;
}
- (void)setShowBack:(BOOL)showBack
{
if (showBack) {
_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
_backButton.frame = CGRectMake(0, 0, 20, 44);
[_backButton setImage:[UIImage imageNamed:@"back-white"] forState:UIControlStateNormal];
[_backButton addTarget:self action:@selector(baseBack) forControlEvents:UIControlEventTouchUpInside];
_backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:_backButton];
backItem.style = UIBarButtonItemStylePlain;
self.navigationItem.leftBarButtonItem = backItem;
}else{
self.navigationItem.hidesBackButton = YES;
}
//解決titleView不能居中的問(wèn)題
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 35, 35);
button.hidden = YES;
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = right;
}
- (void)setShowLeftButton:(BOOL)showLeftButton
{
if (showLeftButton) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 35, 35);
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[button addTarget:self action:@selector(leftBarButtonItemTouchedUpInSide) forControlEvents:UIControlEventTouchUpInside];
}
}
- (void)setShowRightButton:(BOOL)showRightButton
{
if (showRightButton) {
_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
_rightButton.frame = CGRectMake(0, 0, 30, 30);
_rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15);
//可以根據(jù)自己想要的圖標(biāo)在目標(biāo)控制器里設(shè)置
// [_rightButton setImage:[UIImage imageNamed:@"more_icon_forum"] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightButton];
[_rightButton addTarget:self action:@selector(rightBarButtonItemTouchedUpInSide) forControlEvents:UIControlEventTouchUpInside];
}
}
//touch up inside響應(yīng)按鈕方法
- (void)leftBarButtonItemTouchedUpInSide
{
//根據(jù)自己需要實(shí)現(xiàn)
NSLog(@"leftBarButtonItemTouchedUpInSide未實(shí)現(xiàn)");
}
- (void)rightBarButtonItemTouchedUpInSide
{
//根據(jù)自己需要實(shí)現(xiàn)
NSLog(@"rightBarButtonItemTouchedUpInSide未實(shí)現(xiàn)");
}
7.創(chuàng)建tabbar Items 控制器,tabbar有幾個(gè)items就創(chuàng)建幾個(gè),繼承自BaseViewController
-
8.創(chuàng)建tabbar控制器,繼承自UITabBarController
~8.1 遵守協(xié)議:UITabBarDelegate,UITabBarControllerDelegate
~8.2 items控制器組@property(nonatomic,strong)NSMutableArray *viewControllers;
~8.3 初始化控制器
- (void)setUpControllers
{
SJOneViewController * oneVC = [[SJOneViewController alloc] init];
MLNavigationController *oneNav = [[MLNavigationController alloc] initWithRootViewController:oneVC];
SJTwoViewController * twoVC = [[SJTwoViewController alloc] init];
MLNavigationController *twoNav = [[MLNavigationController alloc] initWithRootViewController:twoVC];
SJThreeViewController *threeVC = [[SJThreeViewController alloc] init];
MLNavigationController *threeNav = [[MLNavigationController alloc] initWithRootViewController:threeVC];
[self.viewControllers addObject:oneNav];
[self.viewControllers addObject:twoNav];
[self.viewControllers addObject:threeNav];
}
~8.4 添加子控制器,初始化tabbarItem 方法
- (void)addController:(UIViewController *)viewController title:(NSString *)title normolImageName:(NSString *)normalImageName selectImageName:(NSString *)selectImageName
{
viewController.tabBarItem.title = title;
UIImage *normalImage = [UIImage imageNamed:normalImageName];
normalImage = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
viewController.tabBarItem.image = normalImage;
UIImage *selectedImage = [UIImage imageNamed:selectImageName];
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
viewController.tabBarItem.selectedImage = selectedImage;
[self addChildViewController:viewController];
}
~8.5 設(shè)置文字不同狀態(tài)下屬性方法
- (void)setUpTabbarItemTextAttributes
{
//普通狀態(tài)下的文字屬性
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
normalAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.00f green:0.64f blue:0.51f alpha:1.00f];
//選中狀態(tài)下的文字屬性
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
UITabBarItem *tabbarItem = [UITabBarItem appearance];
[tabbarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
[tabbarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}
~8.6 UITabBarControllerDelegate代理方法(可選實(shí)現(xiàn))
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
if ([viewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)viewController;
UIViewController *vc = [nav.viewControllers objectAtIndex:0];
NSLog(@"current viewcontroller is %@", vc);
}
return YES;
}
~8.7 tabbar items 各屬性賦值(核心代碼示例)
NSArray *array = @[@{@"title":@"殺敵",@"normalImageName":@"battle_win_kill",@"selectImageName":@"battle_lose_kill"},
@{@"title":@"死亡",@"normalImageName":@"battle_win_death",@"selectImageName":@"battle_lose_death"},
@{@"title":@"助攻",@"normalImageName":@"battle_win_assist",@"selectImageName":@"battle_lose_assist"}];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSDictionary *dic = (NSDictionary *)obj;
UIViewController *vc = self.viewControllers[idx];
NSString *title = [dic objectForKey:@"title"];
NSString *normalImageName = [dic objectForKey:@"normalImageName"];
NSString *selectImageName = [dic objectForKey:@"selectImageName"];
[self addController:vc title:title normolImageName:normalImageName selectImageName:selectImageName];
}];
[self setUpTabbarItemTextAttributes];
self.delegate = self;
- 9.至此基本完畢,在目標(biāo)控制器里面,可以選擇性的實(shí)現(xiàn)基類(lèi)的一些方法來(lái)自定義目標(biāo)控制器,如:
①目標(biāo)控制器的navTitle
<pre>[self setNavTitle:@"殺敵"];</pre>
②是否有返回按鈕,默認(rèn)無(wú)
<pre>[self setShowRightButton:YES];</pre>
③設(shè)置navigationItem.rightBarButtonItem的圖標(biāo)
<pre>[self.rightButton setImage:[UIImage imageNamed:@"battle_win_death"] forState:UIControlStateNormal];</pre>
④rightBarButtonItem的點(diǎn)擊響應(yīng)放法
<pre>- (void)rightBarButtonItemTouchedUpInSide{
//your code
}</pre>