【低耦合集成TabBarController】最低只需传两个数组即可完成主流App框架搭建

【低耦合集成TabBarController】最低只需传两个数组即可完成主流App框架搭建,第1张

概述Github仓库地址戳这里 导航 与其他自定义TabBarController的区别 集成后的效果 使用CYLTabBarController 第一步:使用cocoaPods导入CYLTabBarController 第二步:设置CYLTabBarController的两个数组:控制器数组和TabBar属性数组 第三步:将CYLTabBarController设置为window的RootViewC

Github仓库地址戳这里



导航

与其他自定义TabBarController的区别

集成后的效果

使用CYLTabBarController

第一步:使用cocoaPods导入CYLTabBarController

第二步:设置CYLTabBarController的两个数组:控制器数组和TabBar属性数组

第三步:将CYLTabBarController设置为window的RootViewController

第四步(可选):创建自定义的形状不规则加号按钮

补充说明

与其他自定义TabbarController的区别
- 特点 解释
1 低耦合 与业务完全分离,最低只需传两个数组即可完成主流App框架搭建
2 Tabbar内均使用系统的TabbarItem ,并非UIbutton或UIVIEw 无需反复调“间距位置等”来接近系统效果
3 自动监测是否需要添加“加号”按钮,并能自动设置位置 CYLTabBarController 既支持类似微信的“中规中矩”的 TabbarController 样式,并且默认就是微信这种样式,同时又支持类似“微博”或“淘宝闲鱼”这种具有不规则加号按钮的 TabbarController 。想支持这种样式,只需自定义一个加号按钮,CYLTabBarController 能检测到它的存在并自动将 tabbar 排序好,无需多余 *** 作,并且也预留了一定接口来满足自定义需求。“加号”按钮的样式、frame均在自定义的类中独立实现,不会涉及tabbar相关设置。
4 即使加号按钮超出了tabbar的区域,超出部分依然能响应点击事件 红线内的区域均能响应tabbar相关的点击事件,
5 支持CocoaPods 容易集成

(学习交流群:498865024)

集成后的效果:
既支持默认样式 同时也支持创建自定义的形状不规则加号按钮
本仓库配套Demo的效果: 另一个Demo 使用CYLTabbarController实现了微博Tabbar框架,效果如下
使用CYLTabBarController

四步完成主流App框架搭建:

第一步:使用cocoaPods导入CYLTabBarController

第二步:设置CYLTabBarController的两个数组:控制器数组和TabBar属性数组

第三步:将CYLTabBarController设置为window的RootViewController

第四步(可选):创建自定义的形状不规则加号按钮

第一步:使用cocoaPods导入CYLTabbarController

Podfile 中如下导入:

pod 'CYLTabbarController'

然后使用 cocoaPods 进行安装:

建议使用如下方式:

# 不升级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 的类,要求和步骤:

实现 CYLPlusbuttonSubclassing 协议

子类将自身类型进行注册,一般可在 applicationapplicationDelegate 方法里面调用 [YourClass registerSubClass] 或者在子类的 +load 方法中调用:

+(voID)load {   [super registerSubclass];}

协议提供了两个可选方法:

+ (NSUInteger)indexOfPlusbuttonInTabbar;+ (CGfloat)multiplerInCenterY;

作用分别是:

+ (NSUInteger)indexOfPlusbuttonInTabbar;

用来自定义加号按钮的位置,如果不实现默认居中,但是如果 tabbar 的个数是奇数则必须实现该方法,否则 CYLTabbarController 会抛出 exception 来进行提示。

+ (CGfloat)multiplerInCenterY;

该方法是为了调整自定义按钮中心点Y轴方向的位置,建议在按钮超出了 tabbar 的边界时实现该方法。返回值是自定义按钮中心点Y轴方向的坐标除以 tabbar 的高度,如果不实现,会自动进行比对,预设一个较为合适的位置,如果实现了该方法,预设的逻辑将失效。

详见Demo中的 CYLPlusbuttonSubclass 类的实现。

补充说明

如果想更进一步的自定义 Tabbar 样式可在 -application:dIDFinishLaunchingWithOptions: 方法中设置

/***  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程序犭袁
原创文章,版权声明:自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 3.0

总结

以上是内存溢出为你收集整理的【低耦合集成TabBarController】最低只需传两个数组即可完成主流App框架搭建全部内容,希望文章能够帮你解决【低耦合集成TabBarController】最低只需传两个数组即可完成主流App框架搭建所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1083242.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存