iOS-tabBar切换不同控制器封装(自定义导航+自定义uiviewcontroler+系统自带tabbar+自定义tabbarController)
首先,一个app的搭建环境非常重要。既要实现基本功能,又要考虑后期优化的性能。
现在很多应用不仅仅是系统自带的控制器,由于需求复杂,基本上需要自定义多控制器来管理。
一、新建一个BasicNavigationViewController,继承UINavigationController
在这里实现导航外观,方法什么的。
示例代码如下:
二、接着自定义一个BasicTabbarViewController,继承UITabBarController
代码如下:
#import <UIKit/UIKit.h>
@class BasicNavgationViewController;
@interface BasicTabbarViewController : UITabBarController
@property (nonatomic, strong) BasicNavgationViewController *homeNavgationController;
@property (nonatomic, strong) BasicNavgationViewController *mineNavgationController;
@property (nonatomic, strong) BasicNavgationViewController *moreNavgationController;
@end
#import "RootViewController.h"
#import "HomeViewController.h"
#import "MineViewController.h"
#import "MoreViewController.h"
#import "BasicNavgationViewController.h"
#define DMNavgationColor DMRGB(65, 109, 218) //导航颜色
#define DMRGB(r, g, b) [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]
#define DMTableViewGrayColor DMRGB(245, 245, 249) // 亮灰(tableView背景)
@interface BasicTabbarViewController ()<UITabBarControllerDelegate>
@end
@implementation BasicTabbarViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setCustomAppearance];
[self setTabBarController];
}
#pragma mark - 设置TabBarController
- (void)setTabBarController
{
self.delegate = self;
[self setViewControllers:[self viewControllers]];
}
- (NSArray *)viewControllers
{
HomeViewController *homeVC=[[HomeViewController alloc]init];
self.homeNavgationController = [[BasicNavgationViewController alloc]initWithRootViewController:homeVC];
_homeNavgationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"btn_首页_none"] selectedImage:[UIImage imageNamed:@"btn_首页_selected"]];
_homeNavgationController.tabBarItem.selectedImage = [[UIImage imageNamed:@"btn_首页_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
_homeNavgationController.tabBarItem.tag = DMLoginSuccessBackTypeHome;
MineViewController *mineVC=[[MineViewController alloc]init];
self.mineNavgationController = [[BasicNavgationViewController alloc]initWithRootViewController:mineVC];
_mineNavgationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"btn_我的_none"] selectedImage:[UIImage imageNamed:@"btn_我的_selected"]];
_mineNavgationController.tabBarItem.selectedImage = [[UIImage imageNamed:@"btn_我的_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
_mineNavgationController.tabBarItem.tag = DMLoginSuccessBackTypeMine;
MoreViewController *moreVC=[[MoreViewController alloc]init];
self.moreNavgationController = [[BasicNavgationViewController alloc]initWithRootViewController:moreVC];
_moreNavgationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"更多" image:[UIImage imageNamed:@"btn_更多_none"] selectedImage:[UIImage imageNamed:@"btn_更多_selected"]]
;
_moreNavgationController.tabBarItem.selectedImage = [[UIImage imageNamed:@"btn_更多_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
_moreNavgationController.tabBarItem.tag = DMLoginSuccessBackTypeMore;
return @[_homeNavgationController, _mineNavgationController, _moreNavgationController];
// NSArray *controllers = [NSArray arrayWithObjects:_BoutiqueGoodsNavgationController,_CategoryNavgationController,_ShopNavgationController,_orderNavgationController,_MyNavgationController,nil];
//
// self.viewControllers = controllers; 也可以这样代替
}
#pragma mark - 自定义控件外观
- (void)setCustomAppearance
{
/* UINavigationBar */
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
NSDictionary *nNormalDictionary = @{NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:18.0f]};
[[UINavigationBar appearance] setTitleTextAttributes:nNormalDictionary];
// [[UINavigationBar appearance] setTranslucent:NO];
[[UINavigationBar appearance] setBarTintColor:DMNavgationColor];
/* UITarBar */
// [[UITabBar appearance] setTintColor:[UIColor whiteColor]];
/* UITarBarItem */
// 设置正常状态下TabBarItem字体
NSDictionary *normalDictionary = @{NSForegroundColorAttributeName: DMRGB(143, 151, 175),
NSFontAttributeName:[UIFont systemFontOfSize:10.0f]};
[[UITabBarItem appearance] setTitleTextAttributes:normalDictionary forState:UIControlStateNormal];
// 设置选中状态下TabBarItem字体
NSDictionary *selectedDictionary = @{NSForegroundColorAttributeName: DMRGB(68, 112, 224),
NSFontAttributeName:[UIFont systemFontOfSize:10.0f]};
[[UITabBarItem appearance] setTitleTextAttributes:selectedDictionary forState:UIControlStateSelected];
[[UITabBar appearance]setBackgroundColor:DMTableViewGrayColor];
[self.tabBar setClipsToBounds:YES];
}
#pragma mark - UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
return YES;
}
三、最后:自定义一个RootviewController,继承UIViewController,以后从控制器创建都继承于它即可。比如:login控制器,register控制器,HomeViewController,mineViewController,MoreViewController等等等等。