百思不得姐 项目实战
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];
//addChildViewController是UIView的方法给控制器添加子控制器如果是普通控制器视图会被覆盖
[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 修改后运行结果: