0 TabBarItem 是 TabBar 上的item
1 TabBar 是一個UI控件
2 TabBarController 封裝了 tabbar 與多個視圖控制器的切換
注:TabBarController 大于5個就會有 more(盡量不要出現(xiàn),太丑了),與more相關的custom,本文都不涉及
TabBarItem
其父類UIBarItem 不講了
- 基本創(chuàng)建與屬性
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"111"
image:[UIImage imageNamed:@"tabbar_icon0_normal"]
selectedImage:[UIImage imageNamed:@"tabbar_icon0_selected"]];
item1.badgeValue = @"123";// 提示數(shù)字
item1.titlePositionAdjustment = UIOffsetMake(-2, -2);// 文字偏移
TabBar
- 1 基本創(chuàng)建與屬性
// 基本配置
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(10, 100, CGRectGetWidth(self.view.bounds)-20, 44)];
[self.view addSubview:tabBar];
[tabBar setItems:@[item1,item2,item3,item4,item5,item6,item7]];
// 更多屬性
tabBar.barStyle = UIBarStyleDefault;// 黑 和 白
tabBar.translucent = YES;// 透明屬性
tabBar.delegate = self;
tabBar.tintColor = [UIColor redColor];// 無效了。
tabBar.barTintColor = [UIColor yellowColor];// Bar 的color,設置圖片無效
tabBar.selectedImageTintColor = [UIColor greenColor];// 選中圖片的 tintColor
// tabBar.selectionIndicatorImage = [UIImage imageNamed:@"navigation_backIcon"];// 默認的選中圖片
// tabBar.backgroundImage = [UIImage imageNamed:@"navigation_backIcon"];// 背景圖
// tabBar.shadowImage = [UIImage imageNamed:@"navigation_backIcon"];// 上面的1px陰影
// item 定位
// 雖然注釋寫了使用這種定位,可以使用上面2個屬性,實測無效,請告訴
tabBar.itemWidth = 200.;
tabBar.itemSpacing = 10.;
tabBar.itemPositioning = UITabBarItemPositioningCentered;我為啥
```
- 2 代理
這里只寫這個,另外幾個都是與 more的自定義有關,不想研究了。
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
tabBar.selectedItem.badgeValue = @([tabBar.selectedItem.badgeValue integerValue] +1).stringValue;
tabBar.selectedItem.titlePositionAdjustment = UIOffsetMake(2, 2);
}
## TabBarController
封裝了 tabbar 與多個視圖控制器的切換
- 1 基本創(chuàng)建與屬性
[self setViewControllers:@[] animated:YES];// 設置childViewController
// 所有子控制器
[self.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@",obj);
}];
// self.tabBar;// 獲取tabBar,主要對這個做處理,
self.delegate = self;
- 2 代理
pragma mark - 一般使用
// 是否可以點擊
-
(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
BOOL isLogin = NO;
// 比如沒有登錄不能進 B控制器
if (!isLogin && [viewController isKindOfClass:[BViewController class]]) {
NSLog(@"你還沒登錄,不能看");
return NO;
}return YES;
}
// 點擊選中時
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(@"%zi",self.selectedIndex);
NSLog(@"%@",self.selectedViewController);
}
pragma mark - 下面的一般不用到
// 更多 開始編輯
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers {
}
// 更多 將要結束編輯
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed {
}
// 更多 已經結束編輯
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed {
}
// 屏幕旋轉是,tabBarController 支持的方向,多選
- (UIInterfaceOrientationMask)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController {
return UIInterfaceOrientationMaskAll;
}
// 子視圖指定方向,還跟子控制器有關,比較復雜。
- (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController *)tabBarController {
return self.selectedViewController.preferredInterfaceOrientationForPresentation;
}
pragma mark - 下面的動畫轉場,再研究,一時半會兒搞不定
//// 自定義 切換 交互式
//- (nullable id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController
// interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController {
//}
//// 自定義 切換 動畫
//- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
// animationControllerForTransitionFromViewController:(UIViewController *)fromVC
// toViewController:(UIViewController *)toVC {
//}
demo:https://github.com/JuYiWei/CZ_Demos
1