UITabBarController基本模板

1.UINavigationController

@interface CommonNavigation : UINavigationController

#import "CommonNavigation.h"

@interface CommonNavigation ()

@end

@implementation CommonNavigation

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.interactivePopGestureRecognizer.delegate = self;
    self.navigationBar.barTintColor = [UIColor redColor];
    self.navigationBar.translucent = NO;
}
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{    
    UIButton * back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];

    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];
    [back sizeToFit];
    //隐藏工具栏
    if (self.viewControllers.count) {
        [viewController setHidesBottomBarWhenPushed:YES];
    }
    [super pushViewController:viewController animated:animated];
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return self.viewControllers.count >1;
}
-(void)backAction
{
    if (self.viewControllers.count > 1) {
        [self popViewControllerAnimated:YES];
    }else
    {
        [self dismissViewControllerAnimated:NO completion:nil];
    }
}
//修改statusbar的颜色
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end

2.UITabBarController

@interface CommonTabBar : UITabBarController

#import "CommonTabBar.h"
#import "CommonNavigation.h"

@interface CommonTabBar ()

@end

@implementation CommonTabBar

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTabBar];
}

-(void)initTabBar{
    CommonNavigation *firstVC = [[CommonNavigation alloc]initWithRootViewController:[FirstVC  new]];
    
   CommonNavigation *secondVC = [[CommonNavigation alloc]initWithRootViewController:[SecondVC  new]];
    
    
    [self setOneChildController:firstVC title:@"首页" nomarlImage:@"imageNormal" selectedImage:@"imageSelect"];
    [self setOneChildController:secondVC title:@"第二页" nomarlImage:@"imageNormal" selectedImage:@"imageSelect"];

}

-(void)setOneChildController:(UIViewController *)vc title:(NSString *)title nomarlImage:(NSString *)nomarlImageStr selectedImage:(NSString *)selectedImageStr{
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:nomarlImageStr];
    //系统会吧图片渲染成蓝色,设置为original则不会被渲染
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageStr] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self addChildViewController:vc];
}

@end

3.调用:

3.1 如果是作为主入口
先去掉默认指定(info.plist里把下面这句去掉)


然后再在APPdelegate哪里重新指定

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
  //调试用
    self.window.backgroundColor = [UIColor redColor];

    self.window.rootViewController = [[CommonTabBar alloc]init];
    
    //显示窗口
    [self.window makeKeyAndVisible];
    return YES;
}

3.2 如果不是做为主入口,而是作为新的业务板块,那么就用模态推出。

你可能感兴趣的:(UITabBarController基本模板)