ios ~ UITabBarController tabBar背

作者: 阳光下的叶子呵 | 来源:发表于2022-07-27 10:50 被阅读0次

    tabbar 做个笔记。

    • 改变tabbar的选中的item的字体颜色

      self.tabbar.tintColor = [UIColor redColor];
      
    • 改变tabbar未选中的item的字体颜色

      self.tabbar.unselectedItemTintColor = [UIColor yellowColor];
      
    • 改变tabbar的背景颜色

      • 方法1
      self.tabbar.barTintColor = [UIColor redColor];
      self.tabbar.translucent =  NO;
      
      • 方法2
      [[UITabBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]];
      
      
    • self.tabbar.translucent = NO干了什么

      • YES的时候,tabbar的视图层级是
        UITabBar-->UIBarBackground-->UIVisualEffectView-->UIVisualEffectBackdropView-->UIVisualEffectSubview-->UIVisualEffectSubview (决定tabbar颜色的视图)
      • NO的时候,tabbar的视图层级是
        UITabBar-->UIBarBackground-->UIImageView (决定tabbar颜色的视图)
      • YES还是NO,从颜色效果上,我没看出区别
      • translucent = NO 对UICollectionView截屏的影响,具体见第十一条
    • 使用方法2的时候,视图层级self.tabbar.translucent = NO一摸一样,都是3层

    • 改变tabbar的背景颜色,又看到下面这个方法

           UIView *color_view = [[UIView alloc]initWithFrame:self.tabBar.bounds];
           color_view.backgroundColor = [UIColor redColor];
           [self.tabBar insertSubview:color_view atIndex:0];
      
      

      这个方法只是改变tabbar部分的背景颜色,tabbar到底部的安全区safeArea有一条是改变不了的。比如使用上面的方法设置tabbarredcolor,tabbar下面的安全区有一条留白。

    // 这是当self.tabBar.translucent = YES时,tabBar的UIVisualEffectView背景色
        if (@available (iOS 15.0, *)) {
            // iOS 15.0 及以上
            UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
            [appearance configureWithOpaqueBackground];
            appearance.backgroundColor = RGBA(236, 231, 222, 1);
            
            self.tabBar.standardAppearance = appearance;
            self.tabBar.scrollEdgeAppearance = self.tabBar.standardAppearance;
        } else {
            
            self.tabBar.barTintColor = RGBA(236, 231, 222, 1);
        }
        /**
         
        // 当tabBar.translucent = NO 时,可以直接设置 tabBar.backgroundColor:
         
        self.tabBar.translucent = NO;
        self.tabBar.barTintColor = RGBA(236, 231, 222, 1);
        self.tabBar.backgroundColor = UIColor.redColor;
         */
        /**
        // swift:
        
        if #available(iOS 15.0, *) {
           let appearance = UITabBarAppearance()
           appearance.configureWithOpaqueBackground()
           appearance.backgroundColor = customColor
    
           self.tabController.tabBar.standardAppearance = appearance
           self.tabController.tabBar.scrollEdgeAppearance = self.tabController.tabBar.standardAppearance
        }
         */
    

    相关文章

      网友评论

        本文标题:ios ~ UITabBarController tabBar背

        本文链接:https://www.haomeiwen.com/subject/ufpoirtx.html