【问题标题】:Setting tabBarAppearance makes it so UITabBarItem doesn't respect `imageInsets`设置 tabBarAppearance 使得 UITabBarItem 不尊重`imageInsets`
【发布时间】:2022-01-24 03:13:01
【问题描述】:

当我设置 UITabBar 的外观时,它使得 UITabBarItem 的 imageInsetstitlePositionAdjustment 在 iOS 15 上不再受到尊重。删除线 [UITabBar appearance].standardAppearance = tabBarAppearance; 使其成为图像和标签阵容但背景颜色无法设置。有没有人遇到过这样的问题并解决了?

代码:

UITabBarAppearance *tabBarAppearance = [[UITabBarAppearance alloc] init];
[tabBarAppearance configureWithOpaqueBackground];
[UITabBar appearance].standardAppearance = tabBarAppearance;
[UITabBar appearance].scrollEdgeAppearance = tabBarAppearance;

static let tabBarImageInsets: UIEdgeInsets = UIEdgeInsets(top: 4.0, left: 0, bottom: -4.0, right: 0)
static let titlePositionAdjustmentValue: UIOffset = UIOffset(horizontal: 0.0, vertical: -8.0)
tabBarItem.imageInsets = tabBarImageInsets
tabBarItem.titlePositionAdjustment = titlePositionAdjustmentValue

图片:

Tab Bar with appearance and misaligned label

Tab Bar without appearance, properly aligned label and image, and no background

【问题讨论】:

    标签: ios swift objective-c uitabbar


    【解决方案1】:

    我在实际阅读 Apple 文档后解决了这个问题:

    if (@available(iOS 15.0, *)) {
            UINavigationBarAppearance *navBarAppearance = [[UINavigationBarAppearance alloc] init];
            [navBarAppearance configureWithOpaqueBackground];
            [UINavigationBar appearance].standardAppearance = navBarAppearance;
            [UINavigationBar appearance].compactAppearance = navBarAppearance;
            [UINavigationBar appearance].scrollEdgeAppearance = navBarAppearance;
    
            UITabBarAppearance *tabBarAppearance = [[UITabBarAppearance alloc] init];
            [tabBarAppearance configureWithOpaqueBackground];
            [UITabBar appearance].standardAppearance = tabBarAppearance;
            [UITabBar appearance].scrollEdgeAppearance = tabBarAppearance;
    
            UITabBarItemAppearance *tabBarItemAppearance = [[UITabBarItemAppearance alloc] init];
    
            tabBarItemAppearance.normal.titleTextAttributes = [[RRUIDesignToken.font nav] makeTextAttributes];
            if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
                tabBarItemAppearance.normal.titlePositionAdjustment = UIOffsetMake(0.0, -[RRUIDesignToken.size paddingXsmall]);
            } else {
                tabBarItemAppearance.normal.titlePositionAdjustment = UIOffsetMake(0.0, -[RRUIDesignToken.size paddingXxsmall]);
            }
    
            tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance;
            tabBarAppearance.compactInlineLayoutAppearance = tabBarItemAppearance;
            tabBarAppearance.inlineLayoutAppearance = tabBarItemAppearance;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多