- 参考链接:关于简单的自定义NavigationController和TabBarController
- 感谢作者的分享。
自定义NavigationViewController
- 代码如下
- (void)viewDidLoad {
[super viewDidLoad];
self.interactivePopGestureRecognizer.delegate = self;
}
+(void)initialize
{
//设置导航items数据主题
[self setupNavigationItemsTheme];
//设置导航栏主题
[self setupNavigationBarTheme];
}
#pragma mark - 设置导航栏数据主题
+(void)setupNavigationItemsTheme
{
UIBarButtonItem *barButtonItem = [UIBarButtonItem appearance];
//设置字体颜色
[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor],NSFontAttributeName : [UIFont systemFontOfSize:14]} forState:UIControlStateNormal];
[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateHighlighted];
[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor]} forState:UIControlStateDisabled];
}
#pragma mark - 设置导航栏主题
+(void)setupNavigationBarTheme
{
UINavigationBar *bar = [UINavigationBar appearance];
//设置导航栏的title属性
[bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
//设置导航栏颜色
[bar setBarTintColor:[UIColor colorWithRed:0.5 green:0.5 blue:0.8 alpha:1]];
//设置导航栏背景图
// UIImage *image = [UIImage imageNamed:@"Home-Page"];
// [bar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
#pragma mark - 拦截所有push方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count > 0) {
// 如果navigationController的字控制器个数大于两个就隐藏,底部工具栏
viewController.hidesBottomBarWhenPushed = YES;
//设置返回按钮
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
}
[super pushViewController:viewController animated:animated];
}
/**
* 点击返回按钮时调用
* 返回上一个界面
*/
-(void)back
{
[super popViewControllerAnimated:YES];
}
/**
* 手势识别器对象会调用这个代理方法来决定手势是否有效
*
* @return YES : 手势有效, NO : 手势无效
*/
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
// 手势何时有效 : 当导航控制器的子控制器个数 > 1就有效
return self.childViewControllers.count > 1;
}
自定义TabBarController
- 代码如下
- (void)viewDidLoad {
[super viewDidLoad];
[self addAllViewControllers];
}
#pragma mark - 添加所有自控制器
- (void)addAllViewControllers
{
//将需要绑定的页面个添加进来,这里可以抽取为一个方法添加,从而简化代码。
//实例化一个ViewController
MeViewController *vc = [[MeViewController alloc] init];
//实例化一个VC为根视图的MyNavigationViewController
MyNavigationViewController *Nvc = [[MyNavigationViewController alloc] initWithRootViewController:vc];
//设置tabBarItem的标题
vc.tabBarItem.title = @"home";
//设置tabBarItem的图片
vc.tabBarItem.image = [UIImage imageNamed:@"244"];
//设置tabbaritem选中时的显示的图片
vc.tabBarItem.selectedImage = [UIImage imageNamed:@"244-拷贝"];
//添加MyNavigationViewController到tabbar
[self addChildViewController:Nvc];
//实例化一个ViewController
ViewController *vc2 = [[ViewController alloc] init];
//实例化一个VC为根视图的MyNavigationViewController
MyNavigationViewController *Nvc2 = [[MyNavigationViewController alloc] initWithRootViewController:vc2];
//设置tabBarItem的标题
vc2.tabBarItem.title = @"ME";
//设置tabBarItem的图片
vc2.tabBarItem.image = [UIImage imageNamed:@"26270"];
//设置tabbaritem选中时的显示的图片
vc2.tabBarItem.selectedImage = [UIImage imageNamed:@"18281-拷贝"];
//添加MyNavigationViewController到tabbar
[self addChildViewController:Nvc2];
}
+(void)initialize
{
//设置UITabBarItem主题
[self setupTabBarItemTheme];
//设置uitabar主题
[self setupTabBarTheme];
}
#pragma mark - 设置tabbarItem的主题
+(void)setupTabBarItemTheme
{
UITabBarItem *tabBarItem = [UITabBarItem appearance];
/****设置文字属性****/
//普通状态颜色为黑色,字体大小12
[tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0f],NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateNormal];
//选中状态颜色为橙色,字体大小12
[tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0f],NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
//高亮状态
[tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0f]} forState:UIControlStateHighlighted];
//不可用状态
[tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0f]} forState:UIControlStateDisabled];
}
#pragma mark - 设置tabbarItem的主题
+ (void)setupTabBarTheme
{
//这里可以更换自定义tabbar
//[self setValue:[[XMGTabBar alloc] init] forKeyPath:@"tabBar"];}
}
创建界面一
- 代码如下
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"界面一";
// Do any additional setup after loading the view.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"跳转" forState:UIControlStateNormal];
// [btn.titleLabel setBackgroundColor:[UIColor whiteColor]];
[btn.titleLabel setTextColor:[UIColor whiteColor]];
[btn setBackgroundColor:[UIColor orangeColor]];
btn.frame = CGRectMake(100, 200, 80, 40);
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//点击跳转按钮时调用
-(void)btnClick
{
ViewController *vc = [[ViewController alloc] init];
[vc.view setBackgroundColor:[UIColor colorWithRed:0.6 green:0.5 blue:0.7 alpha:1]];
[self.navigationController pushViewController:vc animated:YES];
}
创建界面二
- 代码如下
- (void)viewDidLoad {
[super viewDidLoad];
/ /设置标题
self.navigationItem.title = @"界面二";
//设置背景颜色
self.view.backgroundColor = [UIColor grayColor];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
最终效果
2016-09-10 at 下午7.42.gif
网友评论
在“#pragma mark - 拦截所有push方法”中有个小错误,最后使用父类的方法 [super pushViewController:viewController animated:YES];
应该将"YES"替换成"animated",因为不是所有push都需要动画效果的,是否需要动画效果应该由上一个VC决定。