UITabBar

UITabBar : UIView
本身就是个 View,只是经常放在 TabBarController 中,单独也可以拿出来使用;

基本初始化

    self.tabbar = [[UITabBar alloc] initWithFrame:CGRectMake(20, 200, 300, 40)];
    [self.view addSubview:self.tabbar];

    UITabBarItem *barItem0 = [[UITabBarItem alloc] initWithTitle:@"111" image:[UIImage imageNamed:@"tab_0_normal"] selectedImage:[UIImage imageNamed:@"tab_0_selected"]];
    UITabBarItem *barItem1 = [[UITabBarItem alloc] initWithTitle:@"222" image:[UIImage imageNamed:@"tab_1_normal"] selectedImage:[UIImage imageNamed:@"tab_1_selected"]];
    self.tabbar.items = @[barItem0 ,barItem1];

tabBar 颜色与背景

    self.tabbar.translucent = YES;// 模糊处理
    self.tabbar.barStyle = UIBarStyleBlack;// 默认 2 选一
    self.tabbar.barTintColor = [UIColor yellowColor];// 背景颜色
    self.tabbar.backgroundImage = [UIImage imageNamed:@"backgroundImage"];// 背景图片
    self.tabbar.shadowImage = [UIImage imageNamed:@"shadowImage"];// 阴影图片,还必须与 backgroundImage 同时设置,不然设置无效,并使用默认颜色

item 颜色与背景

    self.tabbar.tintColor = [UIColor redColor];// 一般用图片原色,并不喜欢用这个 tintColor
    self.tabbar.unselectedItemTintColor = [UIColor blueColor];// iOS 10,且同上
    self.tabbar.selectionIndicatorImage = [UIImage imageNamed:@"icon"];// 选中 item 的背景

item 布局

    self.tabbar.itemPositioning = UITabBarItemPositioningAutomatic;// 填充方式
    self.tabbar.itemWidth = 50;
    self.tabbar.itemSpacing = 20;

当前 item

UITabBarItem 可以继续深入看

    UITabBarItem *item = self.tabbar.selectedItem;

方法

    [self.tabbar setItems:self.arr];    
  • 编辑相关
    [self.tabbar beginCustomizingItems:self.arr];
    [self.tabbar endCustomizingAnimated:YES];

    BOOL custom = self.tabbar.isCustomizing;// 虽然是属性,也放这里了

代理 UITabBarDelegate

  • 点击
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSLog(@"didSelectItem");
}
  • 编辑,真心没见过有这种 APP
- (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray<UITabBarItem *> *)items {
    NSLog(@"willBeginCustomizingItems");
}

- (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray<UITabBarItem *> *)items {
    NSLog(@"didBeginCustomizingItems");
}

- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed {
    NSLog(@"willEndCustomizingItems");
}

- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed {
    NSLog(@"didEndCustomizingItems");
}