[低耦合集成 TabBarController] 最低只需传两个数组即可完成主流 App 框架搭建 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
stevechen1010
V2EX    iDev

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

  •  1
     
  •   stevechen1010
    ChenYilong 2015-10-26 01:25:09 +08:00 3520 次点击
    这是一个创建于 3716 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Github 仓库地址戳这里

    CocoaPods 版本 v1.0.1

    Objective-C

    iOS6+

    导航

    1. 与其他自定义 TabBarController 的区别
    2. 集成后的效果
    3. 使用 CYLTabBarController
      1. 第一步:使用 cocoaPods 导入 CYLTabBarController
      2. 第二步:设置 CYLTabBarController 的两个数组:控制器数组和 TabBar 属性数组
      3. 第三步:将 CYLTabBarController 设置为 window 的 RootViewController
      4. 第四步(可选):创建自定义的形状不规则加号按钮
    4. 补充说明

    与其他自定义 TabBarController 的区别

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

    (学习交流群: 498865024 )

    集成后的效果:

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

    使用CYLTabBarController

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

    1. 第一步:使用 cocoaPods 导入 CYLTabBarController
    2. 第二步:设置 CYLTabBarController 的两个数组:控制器数组和 TabBar 属性数组
    3. 第三步:将 CYLTabBarController 设置为 window 的 RootViewController
    4. 第四步(可选):创建自定义的形状不规则加号按钮

    第一步:使用 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 的类,要求和步骤:

    1. 实现 CYLPlusButtonSubclassing 协议

    2. 子类将自身类型进行注册,一般可在 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

    5 条回复    2015-10-28 10:30:33 +08:00
    fhefh
        1
    fhefh  
       2015-10-26 09:02:14 +08:00
    mark
    holystrike
        2
    holystrike  
       2015-10-26 09:41:47 +08:00
    马可波罗先

    有 swift 版本咩
    fengjianxinghun
        3
    fengjianxinghun  
       2015-10-26 10:51:22 +08:00
    希望库都是 OC 的,让 swift 死掉
    dorentus
        4
    dorentus  
       2015-10-26 10:57:17 +08:00   1
    @fengjianxinghun Swift 可以使用绝大部分 OC 的库, OC 不能用绝大部分 Swift 的库,不知道是谁会死掉
    johnlui
        5
    johnlui  
       2015-10-28 10:30:33 +08:00
    @fengjianxinghun 苹果都全面转向 Swift 开发自带 APP 了,国外大多数公司的新项目都用 Swift 来开发了,不要抱有幻想了。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2517 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 05:31 PVG 13:31 LAX 21:31 JFK 00:31
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86