【低耦合集成TabBarController】最低只需传两个数组即可完成主流
Github仓库地址戳这里 导航
与其他自定义TabBarController的区别
(学习交流群:498865024) 集成后的效果:
使用CYLTabBarController四步完成主流App框架搭建:
第一步:使用cocoaPods导入CYLTabBarController在 pod 'CYLTabBarController' 然后使用 建议使用如下方式: # 不升级CocoaPods的spec仓库 pod update --verbose 第二步:设置CYLTabBarController的两个数组:控制器数组和TabBar属性数组- (void)setupViewControllers { CYLHomeViewController *firstViewController = [[CYLHomeViewController alloc] init]; UIViewController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController]; CYLSameFityViewController *secondViewController = [[CYLSameFityViewController alloc] init]; UIViewController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController]; CYLTabBarController *tabBarController = [[CYLTabBarController alloc] init]; [self customizeTabBarForController:tabBarController]; [tabBarController setViewControllers:@[ firstNavigationController,secondNavigationController,]]; self.tabBarController = tabBarController; } /* * 在`-setViewControllers:`之前设置TabBar的属性, * */ - (void)customizeTabBarForController:(CYLTabBarController *)tabBarController { NSDictionary *dict1 = @{ CYLTabBarItemTitle : @"首页",CYLTabBarItemImage : @"home_normal",CYLTabBarItemSelectedImage : @"home_highlight",}; NSDictionary *dict2 = @{ CYLTabBarItemTitle : @"同城",CYLTabBarItemImage : @"mycity_normal",CYLTabBarItemSelectedImage : @"mycity_highlight",}; NSArray *tabBarItemsAttributes = @[ dict1,dict2 ]; tabBarController.tabBarItemsAttributes = tabBarItemsAttributes; } 第三步:将CYLTabBarController设置为window的RootViewController- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /* *省略部分: * */ [self.window setRootViewController:self.tabBarController]; /* *省略部分: * */ return YES; } 第四步(可选):创建自定义的形状不规则加号按钮创建一个继承于 CYLPlusButton 的类,要求和步骤:
协议提供了两个可选方法: + (NSUInteger)indexOfPlusButtonInTabBar; + (CGFloat)multiplerInCenterY; 作用分别是: + (NSUInteger)indexOfPlusButtonInTabBar; 用来自定义加号按钮的位置,如果不实现默认居中,但是如果 + (CGFloat)multiplerInCenterY; 该方法是为了调整自定义按钮中心点Y轴方向的位置,建议在按钮超出了 详见Demo中的 补充说明如果想更进一步的自定义 /** * tabBarItem 的选中和不选中文字属性、背景图片 */ - (void)customizeInterface { // 普通状态下的文字属性 NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary]; normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor]; // 选中状态下的文字属性 NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary]; selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor]; // 设置文字属性 UITabBarItem *tabBar = [UITabBarItem appearance]; [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal]; [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateHighlighted]; UITabBar *tabBarAppearance = [UITabBar appearance]; [tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tabbar_background"]]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /* *省略部分: * */ [self.window makeKeyAndVisible]; [self customizeInterface]; return YES; } (更多iOS开发干货,欢迎关注 微博@iOS程序犭袁 ) Posted by 微博@iOS程序犭袁 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |