[需求记录]_自定义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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,823评论 25 709
  • 马兰: 见字如面。 丫头,再过两天就是惊蛰了,天气也开始逐渐转暖,不那么冷了,这对于我们来说无疑是天大的好事情。 ...
    孤邻阅读 1,410评论 0 0
  • "我要是再漂亮一点 悄悄看你时 目光就不会闪躲了。" 渔
    渔鲲阅读 1,228评论 0 0
  • 才突然意识到,如果没有了欲望,那么又何来动力。一直以来我都在想着压制自己的欲望,可能潜意识里觉得欲望是个不好的东西...
    辣土豆阅读 1,885评论 0 0
  • 初识斜杠青年这种说法的时候,心情十分激动,脑海中浮现了一个自由人,左右开弓四处飞的形象。同时被某些人神话得是一天只...
    遇见生命的美好阅读 1,657评论 2 1