iOS初学者

iOS 设置UITabBarItem

2024-01-08  本文已影响0人  上北以北
    //tabbar item在各个样式的下的布局
    
    //上下结构tabbar item布局
    UITabBarItemAppearance *stack_apperance = [[UITabBarItemAppearance alloc] initWithStyle:UITabBarItemAppearanceStyleStacked];
    stack_apperance.normal.titlePositionAdjustment = UIOffsetMake(0, 1);
    stack_apperance.selected.titlePositionAdjustment = UIOffsetMake(0, 1);
    stack_apperance.normal.titleTextAttributes = @{NSForegroundColorAttributeName: color, NSFontAttributeName:UIFont.bb_12};
    stack_apperance.selected.titleTextAttributes = @{NSForegroundColorAttributeName: scolor, NSFontAttributeName:UIFont.bb_12};
    
    //左右结构tabbar item布局(左右结构时字体一定要比上下布局小,否则会由于间隔问题导致文字显示不全)
    UITabBarItemAppearance *inline_apperance = [[UITabBarItemAppearance alloc] initWithStyle:UITabBarItemAppearanceStyleInline];
    inline_apperance.normal.titleTextAttributes = @{NSForegroundColorAttributeName: color, NSFontAttributeName:UIFont.bb_10};
    inline_apperance.selected.titleTextAttributes = @{NSForegroundColorAttributeName: scolor, NSFontAttributeName:UIFont.bb_10};
    
    //左右结构被挤压时的tabbar item布局(某些特殊情况,只会出现在ipad上,比如多屏侧边栏出现时会压缩app的宽度)
    UITabBarItemAppearance *compact_apperance = [[UITabBarItemAppearance alloc] initWithStyle:UITabBarItemAppearanceStyleCompactInline];
    compact_apperance.normal.titleTextAttributes = @{NSForegroundColorAttributeName: color, NSFontAttributeName:UIFont.bb_7};
    compact_apperance.selected.titleTextAttributes = @{NSForegroundColorAttributeName: scolor, NSFontAttributeName:UIFont.bb_7};
    

    //tabbar item样式类
    UITabBarAppearance *bar_a = [UITabBarAppearance new];
    bar_a.stackedLayoutAppearance = stack_apperance;
    bar_a.inlineLayoutAppearance = inline_apperance;
    bar_a.compactInlineLayoutAppearance = compact_apperance;
    
    //tabbar item
    UITabBarItem *tab = [[UITabBarItem alloc] initWithTitle:title image:icon selectedImage:sicon];
    tab.imageInsets = UIEdgeInsetsZero;
    tab.standardAppearance = bar_a;
    tab.scrollEdgeAppearance = bar_a;
上一篇下一篇

猜你喜欢

热点阅读