- | 特色 | 解释 |
---|---|---|
1 | 低耦合 | 与业务彻底分离,最低只需传两个数组便可完成主流App框架搭建 |
2 | TabBar内均使用系统的TabbarItem ,并不是UIButton或UIView | 无需反复调“间距位置等”来接近系统效果 |
3 | 自动监测是否须要添加“加号”按钮,并能自动设置位置 | CYLTabBarController 既支持相似微信的“中规中矩”的 TabBarController 样式,而且默认就是微信这种样式,同时又支持相似“微博”或“淘宝闲鱼”这种具备不规则加号按钮的 TabBarController 。想支持这种样式,只需自定义一个加号按钮,CYLTabBarController 能检测到它的存在并自动将 tabBar 排序好,无需多余操做,而且也预留了必定接口来知足自定义需求。“加号”按钮的样式、frame均在自定义的类中独立实现,不会涉及tabbar相关设置。 |
4 | 即便加号按钮超出了tabbar的区域,超出部分依然能响应点击事件 | 红线内的区域均能响应tabbar相关的点击事件,![]() |
5 | 支持CocoaPods | 容易集成 |
(学习交流群:498865024)3d
既支持默认样式 | 同时也支持建立自定义的形状不规则加号按钮 |
---|---|
![]() |
![]() |
本仓库配套Demo的效果: | 另外一个Demo 使用CYLTabBarController实现了微博Tabbar框架,效果以下 |
---|---|
![]() |
![]() |
四步完成主流App框架搭建:
在 Podfile
中以下导入:
pod 'CYLTabBarController'
而后使用 cocoaPods
进行安装:
建议使用以下方式:
# 不升级CocoaPods的spec仓库 pod update --verbose
- (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; }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /* *省略部分: * */ [self.window setRootViewController:self.tabBarController]; /* *省略部分: * */ return YES; }
建立一个继承于 CYLPlusButton 的类,要求和步骤:
实现 CYLPlusButtonSubclassing
协议
子类将自身类型进行注册,通常可在 application
的 applicationDelegate
方法里面调用 [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