不得姐实战教程 02 配置tabbar

百思不得姐 项目实战

02 配置tabbar

1.打开ViewController.m 文件 单击右键

@interface ViewController ()


@end

ViewController选择 Refactor 中Rename…重构类名为XMGTabBarController

 

2. 打开  XMGTabBarController.h将其父类修改为  UITabBarController

3.打开 XMGTabBarController.m

修改 viewDidLoad  方法:

- (void)viewDidLoad

{

    [super viewDidLoad];

   

 

// 添加子控制器tabbar控制器中创建控制器时就可以设置其tabBarItem的属性了

    UIViewController *vc01 = [[UIViewControlleralloc] init];

    vc01.tabBarItem.title =@"精华";

    vc01.tabBarItem.image = [UIImageimageNamed:@"tabBar_essence_icon"];

    vc01.tabBarItem.selectedImage = [UIImageimageNamed:@"tabBar_essence_click_icon"];

   

    //富文本用于更改NSString的字体样式

    NSMutableDictionary *attrs = [NSMutableDictionarydictionary];

    attrs[NSFontAttributeName] = [UIFontsystemFontOfSize:12];

    attrs[NSForegroundColorAttributeName] = [UIColorgrayColor];

    [vc01.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];

   

    NSMutableDictionary *selectedAttrs = [NSMutableDictionarydictionary];

    selectedAttrs[NSFontAttributeName] = [UIFontsystemFontOfSize:12];

    selectedAttrs[NSForegroundColorAttributeName] = [UIColordarkGrayColor];

    [vc01.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

   

    //选中tabBarItem里面的图片文字会被自动蓝色渲染

    //   UIImage *image = [UIImageimageNamed:@"tabBar_essence_click_icon"];

    //   image = [imageimageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    //   vc01.tabBarItem.selectedImage = image;

   

    vc01.view.backgroundColor = [UIColorredColor];

   

    //addChildViewControllerUIView的方法给控制器添加子控制器如果是普通控制器视图会被覆盖

    [self addChildViewController:vc01];

   

    UIViewController *vc02 = [[UIViewControlleralloc] init];

    vc02.tabBarItem.title =@"新帖";

    vc02.tabBarItem.image = [UIImageimageNamed:@"tabBar_new_icon"];

    vc02.tabBarItem.selectedImage = [UIImageimageNamed:@"tabBar_new_click_icon"];

    vc02.view.backgroundColor = [UIColorgrayColor];

    [vc02.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];

    [vc02.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

    [self addChildViewController:vc02];

   

    UIViewController *vc03 = [[UIViewControlleralloc] init];

    vc03.tabBarItem.title =@"关注";

    vc03.tabBarItem.image = [UIImageimageNamed:@"tabBar_friendTrends_icon"];

    vc03.tabBarItem.selectedImage = [UIImageimageNamed:@"tabBar_friendTrends_click_icon"];

    vc03.view.backgroundColor = [UIColorgreenColor];

    [vc03.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];

    [vc03.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

    [self addChildViewController:vc03];

   

    UIViewController *vc04 = [[UIViewControlleralloc] init];

    vc04.tabBarItem.title =@"";

    vc04.tabBarItem.image = [UIImageimageNamed:@"tabBar_me_icon"];

    vc04.tabBarItem.selectedImage = [UIImageimageNamed:@"tabBar_me_click_icon"];

    vc04.view.backgroundColor = [UIColorblueColor];

    [vc04.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];

    [vc04.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

    [self addChildViewController:vc04];

}

 

4.打开AppDelegate.m文件

self.window.rootViewController = [[ViewControlleralloc] init];改为

 self.window.rootViewController = [[XMGTabBarControlleralloc] init];


 

5.防止图片被渲染 还可以通过:

选中 Assets.xcassets/TabBar 下面的任意一副图片   在右侧切换到第三个视图 将 Render As 值改为 Original Image 修改后运行结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值