首先,我是喜欢用纯代码的,所以对StoryBoard 和Xib的一些相应简单设置,就先不做介绍了,后面的话会陆续更新部分关于storyBoard的一些用法以及技巧
UITabBarController 和UINavigationController 都是用来管理多个控制器的一种控制器,现在市面上常用的应用基本都用到了这两种控制器,例如微信,QQ,微博等。
UITabBarController (选项控制器,亦可称为分页控制器)的基本用法
用到UITabBarController基本的设置都很简单,就是添加tabBarItem.title以及tabBarItem.image。这里主要介绍添加图片后的选中图片的设置方法。
第一种方法:选中图片和未选中的图片的区别只是颜色的不同,而且都是纯色图片:
直接上代码:
MainTabBarViewController.h
//
// MainTabBarViewController.h
// Test
//
// Created by 紫竹 on 2017/7/11.
// Copyright © 2017年 CSY. All rights reserved.
//
#import
@interface MainTabBarViewController : UITabBarController
@end
MainTabBarViewController.m
//
// MainTabBarViewController.m
// Test
//
// Created by 紫竹 on 2017/7/11.
// Copyright © 2017年 CSY. All rights reserved.
//
#import "MainTabBarViewController.h"
#import "MainNavigationViewController.h"
#import "HomeViewController.h"
#import "RecordViewController.h"
#import "FindViewController.h"
#import "MeViewController.h"
@interface MainTabBarViewController ()
@end
@implementation MainTabBarViewController
- (void)viewDidLoad {
[super viewDidLoad];
HomeViewController * homeVC = [[HomeViewController alloc]init];
homeVC.tabBarItem.title =@"首页";
homeVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_normal_0"];
RecordViewController * recordVC = [[RecordViewController alloc]init];
recordVC.tabBarItem.title =@"历史";
recordVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_normal_1"];
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
FindViewController * findVC = [[FindViewController alloc]init];
findVC.tabBarItem.title =@"发现";
findVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_normal_2"];
MeViewController * meVC = [[MeViewController alloc]init];
meVC.tabBarItem.title =@"我的";
meVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_normal_3"];
MainNavigationViewController * navi1 = [[MainNavigationViewController alloc]initWithRootViewController:homeVC];
MainNavigationViewController * navi2 = [[MainNavigationViewController alloc]initWithRootViewController:recordVC];
MainNavigationViewController * navi3 = [[MainNavigationViewController alloc]initWithRootViewController:findVC];
MainNavigationViewController * navi4 = [[MainNavigationViewController alloc]initWithRootViewController:meVC];
self.viewControllers = @[navi1,navi2,navi3,navi4];
self.tabBar.tintColor = [UIColor blueColor]; //设置选中时的渲染颜色,主要是渲染tabBarItem的title颜色,如果未添加选中时的图片,则连图片一起渲染
self.tabBar.barTintColor =[UIColor whiteColor]; //tabar的底色设置为纯白
self.tabBar.translucent = NO; // 透明度设置为不透明
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
第二种方法:选中图片和未选中的图片分别设置
//
// MainTabBarViewController.m
// Test
//
// Created by 紫竹 on 2017/7/11.
// Copyright © 2017年 CSY. All rights reserved.
//
#import "MainTabBarViewController.h"
#import "MainNavigationViewController.h"
#import "HomeViewController.h"
#import "RecordViewController.h"
#import "FindViewController.h"
#import "MeViewController.h"
@interface MainTabBarViewController ()
@end
@implementation MainTabBarViewController
- (void)viewDidLoad {
[super viewDidLoad];
HomeViewController * homeVC = [[HomeViewController alloc]init];
homeVC.tabBarItem.title =@"首页";
homeVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_normal_0"];
UIImage * image1 = [UIImage imageNamed:@"tabbar_selected_0"];
homeVC.tabBarItem.selectedImage = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
RecordViewController * recordVC = [[RecordViewController alloc]init];
recordVC.tabBarItem.title =@"历史";
recordVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_normal_1"];
UIImage * image2 = [UIImage imageNamed:@"tabbar_selected_1"];
recordVC.tabBarItem.selectedImage = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
FindViewController * findVC = [[FindViewController alloc]init];
findVC.tabBarItem.title =@"发现";
findVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_normal_2"];
UIImage * image3 = [UIImage imageNamed:@"tabbar_selected_2"];
findVC.tabBarItem.selectedImage = [image3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
MeViewController * meVC = [[MeViewController alloc]init];
meVC.tabBarItem.title =@"我的";
meVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_normal_3"];
UIImage * image4 = [UIImage imageNamed:@"tabbar_selected_3"];
meVC.tabBarItem.selectedImage = [image4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
MainNavigationViewController * navi1 = [[MainNavigationViewController alloc]initWithRootViewController:homeVC];
MainNavigationViewController * navi2 = [[MainNavigationViewController alloc]initWithRootViewController:recordVC];
MainNavigationViewController * navi3 = [[MainNavigationViewController alloc]initWithRootViewController:findVC];
MainNavigationViewController * navi4 = [[MainNavigationViewController alloc]initWithRootViewController:meVC];
self.viewControllers = @[navi1,navi2,navi3,navi4];
self.tabBar.tintColor = [UIColor redColor]; //设置选中时的渲染颜色,主要是渲染tabBarItem的title颜色,如果未添加选中时的图片,则连图片一起渲染
self.tabBar.barTintColor =[UIColor whiteColor]; //tabar的底色设置为纯白
self.tabBar.translucent = NO; // 透明度设置为不透明
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
####### 以上两种方法可以封装为一个方法如下:
//
// MainTabBarViewController.m
// Test
//
// Created by 紫竹 on 2017/7/11.
// Copyright © 2017年 CSY. All rights reserved.
//
#import "MainTabBarViewController.h"
#import "MainNavigationViewController.h"
#import "HomeViewController.h"
#import "RecordViewController.h"
#import "FindViewController.h"
#import "MeViewController.h"
@interface MainTabBarViewController ()
@end
@implementation MainTabBarViewController
- (void)viewDidLoad {
[super viewDidLoad];
HomeViewController * homeVC = [[HomeViewController alloc]init];
RecordViewController * recordVC = [[RecordViewController alloc]init];
FindViewController * findVC = [[FindViewController alloc]init];
MeViewController * meVC = [[MeViewController alloc]init];
//第一种 自动渲染选中效果
MainNavigationViewController * navi1 = [self addViewController:homeVC title:@"首页" imageName:@"tabbar_normal_0" selectImage:@""];
MainNavigationViewController * navi2 = [self addViewController:recordVC title:@"首页" imageName:@"tabbar_normal_1" selectImage:@""];
MainNavigationViewController * navi3 = [self addViewController:findVC title:@"首页" imageName:@"tabbar_normal_2" selectImage:@""];
MainNavigationViewController * navi4 = [self addViewController:meVC title:@"首页" imageName:@"tabbar_normal_3" selectImage:@""];
//第二种 设置选中图片
// MainNavigationViewController * navi1 = [self addViewController:homeVC title:@"首页" imageName:@"tabbar_normal_0" selectImage:@"tabbar_selected_0"];
// MainNavigationViewController * navi2 = [self addViewController:recordVC title:@"首页" imageName:@"tabbar_normal_1" selectImage:@"tabbar_selected_1"];
// MainNavigationViewController * navi3 = [self addViewController:findVC title:@"首页" imageName:@"tabbar_normal_2" selectImage:@"tabbar_selected_2"];
// MainNavigationViewController * navi4 = [self addViewController:meVC title:@"首页" imageName:@"tabbar_normal_3" selectImage:@"tabbar_selected_3"];
self.viewControllers = @[navi1,navi2,navi3,navi4];
self.tabBar.tintColor = [UIColor redColor]; //设置选中时的渲染颜色,主要是渲染tabBarItem的title颜色,如果未添加选中时的图片,则连图片一起渲染
self.tabBar.barTintColor =[UIColor whiteColor]; //tabar的底色设置为纯白
self.tabBar.translucent = NO; // 透明度设置为不透明
}
-(MainNavigationViewController*)addViewController:(UIViewController*)viewController title:(NSString*)title imageName:(NSString*)image selectImage:(NSString*)selectImage{
viewController.tabBarItem.title =title;
viewController.tabBarItem.image = [UIImage imageNamed:image];
if (selectImage.length) {
UIImage * selectIm = [UIImage imageNamed:selectImage];
viewController.tabBarItem.selectedImage = [selectIm imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; //原图不加渲染效果
}
MainNavigationViewController * navi = [[MainNavigationViewController alloc]initWithRootViewController:viewController];
return navi;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
UINavigationController使用方法
导航控制器用的是最普遍的,所以我们可以自己继承UINavigationController,并额外添加我们需要的一些设置效果,这样就不需要各个vc里面去单独设置了
废话不多说,直接上代码:
//
// MainNavigationViewController.h
// Test
//
// Created by 紫竹 on 2017/7/11.
// Copyright © 2017年 CSY. All rights reserved.
//
#import
@interface MainNavigationViewController : UINavigationController
@end
//
// MainNavigationViewController.m
// Test
//
// Created by 紫竹 on 2017/7/11.
// Copyright © 2017年 CSY. All rights reserved.
//
#import "MainNavigationViewController.h"
@interface MainNavigationViewController ()
@end
@implementation MainNavigationViewController
/**
* 第一次使用这个类的时候会调用(1个类只会调用1次)
*/
+ (void)initialize
{
UIBarButtonItem *item = [UIBarButtonItem appearance];
item.tintColor = [UIColor whiteColor];
// 设置文字属性
NSDictionary *itemAttribute=@{NSForegroundColorAttributeName:[UIColor blackColor],NSShadowAttributeName:[[NSShadow alloc]init]};
[item setTitleTextAttributes:itemAttribute forState:UIControlStateNormal];
[item setTitleTextAttributes:itemAttribute forState:UIControlStateHighlighted];
// 取出appearance对象
UINavigationBar *navBar = [UINavigationBar appearance];
navBar.barTintColor = [UIColor whiteColor];
navBar.translucent = NO;
//去掉navigationBar 下面的黑线
// [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
// [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
// 设置标题属性
NSDictionary * textAttrs =@{NSForegroundColorAttributeName:[UIColor blackColor],
NSShadowAttributeName:[[NSShadow alloc]init],
NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
[navBar setTitleTextAttributes:textAttrs];
//[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count > 0) {
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
@end