iOS 重修笔记

重修笔记之设置UITabBarController[基础]

2018-03-03  本文已影响6人  iOS_July
UITabBarController.gif
NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : 
UIViewController <UITabBarDelegate, NSCoding>

创建UITabBarController类

tip ->项目建好后,显示的是默认创建的ViewController

tip ->command + n创建一个继承于UITabBarController的类

tip -> 在AppDelegate.m进行设置:

#import "MBTabBarController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.window.rootViewController = [[MBTabBarController alloc] init];
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

tip ->.m文件里 :

//定义方法
- (void)setupUI{}
- (void)addChildViewController:(UIViewController *)childController 
imageNamed:(NSString *)imageName title:(NSString *)title{}
- (void)setupUI{
    [self addChildViewController:[[MBHomeViewController alloc]init]  imageNamed:@"toolbar_home" title:@"首页"];
    [self addChildViewController:[[MBProfileViewController alloc] init] imageNamed:@"toolbar_me" title:@"我的"];
}
- (void)addChildViewController:(UIViewController *)childController imageNamed:(NSString *)imageName title:(NSString *)title{
    //包裹一个自定义的导航
    MBNavigationController * nav = [[MBNavigationController alloc]initWithRootViewController:childController];
    
    //设置tabBarItem的title
    childController.title = title;
    //设置文字样式
    NSMutableDictionary *textAtts = [NSMutableDictionary dictionary];
    textAtts[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    NSMutableDictionary *selectTextAtts = [NSMutableDictionary dictionary];
    selectTextAtts[NSForegroundColorAttributeName] = [UIColor purpleColor];

    [childController.tabBarItem setTitleTextAttributes:textAtts forState:UIControlStateNormal];
    [childController.tabBarItem setTitleTextAttributes:selectTextAtts forState:UIControlStateSelected];
    
    //设置tabBarItem的图标
    //声明:这张图片按照原来的样子显示出来,不要自动渲染成其他颜色(默认蓝色)
    childController.tabBarItem.image = [UIImage imageNamed:imageName];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_sel", imageName]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [self addChildViewController:nav];
    
}

补充

一般而言,创建项目后,采用于此方法大同小异的方式进行分页管理。但是有些项目也会有特殊性,需要进一步作调整,后续会出一个例子。

上一篇下一篇

猜你喜欢

热点阅读