首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS 15 UITabBarItem自定义

iOS 15 UITabBarItem自定义
EN

Stack Overflow用户
提问于 2021-09-28 10:16:53
回答 1查看 830关注 0票数 1

我有UITabBarConroller子类和自定义项。自定义如下所示:

代码语言:javascript
运行
复制
private func updateTabBar() {
    guard let items = tabBar.items,
        tabViewControllers.count == items.count else {
        return
    }

    for (item, vc) in zip(items, tabViewControllers) {
        // setting items title, image, selectedImage ...
        item.setTitleTextAttributes(Self.normalAttributes, for: .normal)
        item.badgeColor = .customColor
    }
}

这在iOS 14和更早的版本上运行良好。但不是在iOS 15上。我找到了一些关于scrollEdgeAppearance的信息。这可以防止黑色背景。但是设置彩色文本属性和徽章颜色被忽略。问题是如何在UITabBarItem上设置自定义文本颜色和徽章颜色

EN

回答 1

Stack Overflow用户

发布于 2021-09-28 11:55:54

最后,我做到了。我在寻找stackedLayoutAppearance及其属性iconColorbadgeBackgroundColor。根据您的项目,您可能还需要inlineLayoutAppearancecompactInlineLayoutAppearance

代码语言:javascript
运行
复制
@available(iOS 13.0, *)
private static let tabBarAppearance: UITabBarAppearance = {
    let appearance = UITabBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = .customBackgroundColor
            
    appearance.stackedLayoutAppearance.normal.iconColor = .customTitleColorForNormalState
    appearance.stackedLayoutAppearance.normal.titleTextAttributes = normalAttributes
    appearance.stackedLayoutAppearance.selected.titleTextAttributes = normalAttributes
    appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .customBadgeColor
    return appearance
}()

代码语言:javascript
运行
复制
if #available(iOS 13.0, *) {
    item.standardAppearance = Self.tabBarAppearance
    if #available(iOS 15.0, *) {
        item.scrollEdgeAppearance = item.standardAppearance
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69359899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档