iOS 自定義滑動切換TabbarItem 覺得設計丑也要做出來的UI效果。。。
UI丑卻要繼續做的感言:
對UI不滿意的時候,就會覺得丑爆了,時間長了,卻丑習慣了。
論前一陣子Tabbar 多丑,丑得最后不要tabbar了...但是自定義tabbar 和遇到的問題解決的過程可以記錄一下
目標效果:
并有切換效果,但是并沒說清楚,具體切換效果,比如粘滯,彈性?
于是我做了一個彈性的。
看實現效果

一. 原理:
(1)普通切換選擇效果,直接貼在了tabbar上,tabbar再自定義處理圖層
(2)觸發事件是tabbar上的,沒有圖片而已。這么處理也是取巧了,降低了整體自定義難度
二.遇到的問題
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UINavigationController *)viewController
代理方法里,如何區分選中視圖控制器?
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UINavigationController *)viewController { UIViewController *vc = viewController.viewControllers.firstObject; NSInteger tag = vc.tabBarItem.tag; NSLog(@"點擊了 第 %ld 個 tab",tag);//tag來自于視圖初始化時候的賦值 }
- (UIViewController *)mineVC { if (!_mineVC) { _mineVC = [[UIViewController alloc]init]; NSString *title = nil; _mineVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:nil selectedImage:nil]; _mineVC.tabBarItem.tag = 2;//這里對不同的視圖的根控制器進行標記區分 _mineVC.view.backgroundColor = [UIColor redColor]; } return _mineVC; }
在應用不斷交互中,tabbar的合理顯示和隱藏?
//顯示是在主界面上的視圖控制器顯示tabbar其他情況都隱藏,處理方案:
//在視圖控制器基類HFBaseViewController里對tabbar做顯示隱藏的邏輯判斷,并在交互過程自然顯示隱藏不突兀處理
#import <UIKit/UIKit.h> @interface HFBaseViewController : UIViewController @end ////////// @interface HFBaseViewController () @end @implementation HFBaseViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithConfigKey:@"bg_white"]; if (!self.fd_prefersNavigationBarHidden) { [self setDefaultBackButtonItem]; } if (@available(iOS 11.0, *)) { [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; } self.edgesForExtendedLayout = UIRectEdgeNone; //防止tabbar遮擋視圖 }
#pragma mark - 顯示tabbar動畫 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];
if ([FIRWalletHomeVC isTypeLegal:self] || [FIRMarketVC isTypeLegal:self] || [FIRMineVC isTypeLegal:self]) { self.tabBarController.tabBar.alpha = 1.0; self.tabBarController.tabBar.hidden = NO; return ; } [UIView animateWithDuration:0.5 animations:^{ self.tabBarController.tabBar.alpha = 0.0; self.tabBarController.tabBar.hidden = YES; } completion:^(BOOL finished) { //none }]; }
以上。
github 上放了源碼:clone后 直接pod update 就能運行
posted on 2018-09-04 23:28 ACM_Someone like you 閱讀(448) 評論(0) 收藏 舉報
浙公網安備 33010602011771號