设置 tabBarAppearance 使得 UITabBarItem 不遵守 `imageInsets`

Setting tabBarAppearance makes it so UITabBarItem doesn't respect `imageInsets`

当我设置 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

我在实际阅读 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;
    }