专注iOS开发

iOS项目中三级控制器搭建(UITabBarController

2016-07-13  本文已影响109人  爱上火烧的小毛驴

在项目一开始搭建TabBarControler和NavigationController的时候,很多人习惯自定义TabBar。
其实系统的自带的设置TabBarControler、对应的子控制器、对应的Tabbar上的image和title更简单,但有些人出现了图片莫名变得很大或Tabbar上的title跟控制器的Title冲突的问题,下面的解决办法仅作参考,如有问题,请私信我。

#import "MainTabBarController.h"
#import "BaseNavigationController.h"
#import "HomeViewController.h"
#import "FindViewController.h"
#import "AccountViewController.h"

@interface MainTabBarController ()<UITabBarDelegate>
@end
@implementation MainTabBarController
- (void)viewDidLoad {
    [super viewDidLoad];

    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} forState:UIControlStateNormal];
    UIColor *titleHighlightedColor = [UIColor colorWithHexString:MainBlueColor];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       titleHighlightedColor, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateSelected];
    self.tabBar.barTintColor = [UIColor colorWithHexString:MainWhiteColor];
    self.tabBar.translucent = NO;
    [self navigationControllers];
    
}

tabBarItem中的图片变得特别大怎么解决?

在赋图片的时候使用UIImageRenderingModeAlwaysOriginal模式
findNaviVC.tabBarItem.selectedImage=[select2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

- (void)navigationControllers{
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    BaseNavigationController *homeNaviVC = [[BaseNavigationController alloc] initWithRootViewController:homeVC];
    homeNaviVC.tabBarItem.title = @"首页";
    homeNaviVC.tabBarItem.image = [UIImage imageNamed:@"1_1"];
    UIImage *select = [UIImage imageNamed:@"1_2"];
    homeNaviVC.tabBarItem.selectedImage = [select imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    FindViewController *findVC = [[FindViewController alloc] init];
    BaseNavigationController *findNaviVC = [[BaseNavigationController alloc] initWithRootViewController: findVC];
    findNaviVC.tabBarItem.title = @"发现";
    findNaviVC.tabBarItem.image = [UIImage imageNamed:@"1_3"];
    UIImage *select2 = [UIImage imageNamed:@"1_4"];
    findNaviVC.tabBarItem.selectedImage = [select2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    AccountViewController *accountVC = [[AccountViewController alloc] init];
    BaseNavigationController *accountNaviVC = [[BaseNavigationController alloc] initWithRootViewController:accountVC];
    accountNaviVC.tabBarItem.title = @"我的账户";
    accountNaviVC.tabBarItem.image = [UIImage imageNamed:@"1_5"];
    UIImage *select3 = [UIImage imageNamed:@"1_6"];
    accountNaviVC.tabBarItem.selectedImage = [select3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.viewControllers = @[homeNaviVC,findlNaviVC,accountNaviVC];
}

TabBar上的title和控制器导航栏的title不一致怎么解决?

在该tabbarItem对应的导航控制器的RootVC的viewDidLoad中
使用self.navigationItem.title = @"导航栏首页";
不要直接self.title = @"导航栏首页";(会冲突)

更多源码请访问github:https://github.com/zhangjiahuan8888

上一篇下一篇

猜你喜欢

热点阅读