简体   繁体   English

Swift - 如何以编程方式更改 UITabItem 的徽章颜色?

[英]Swift - How to change badge color of UITabItem programatically?

I want to change color of badge of barItem programatically from default red color.我想以编程方式从默认红色更改 barItem 徽章的颜色。

徽章示例

You need to modify it through UITabBar.appearance()需要通过 UITabBar.appearance() 修改

Example:例子:

struct ContentView: View {
    
    var body: some View {
        TabView {
            SomeContent()
                .tabItem {
                    Label("Tab1", systemImage: "star")
                }
            
            SomeContent()
                .tabItem {
                    Label("Tab2", systemImage: "star")
                }
                .badge(10)
        }
        .onAppear() {
            let appearance  = UITabBarAppearance()
            
            //portrait
            appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .green
            
            //landscape
            appearance.inlineLayoutAppearance.normal.badgeBackgroundColor = .blue
            
            UITabBar.appearance().standardAppearance = appearance
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift:如何在tableViewController中以编程方式更改tableHeaderView的边框颜色 - Swift: How to change tableHeaderView's border color programatically in a tableViewController Swift - 如何更改 UITabBarItem 徽章字体? - Swift - How to change UITabBarItem badge font? 如何在 Swift 中以编程方式更改自定义 UITableViewCell 中按钮的背景颜色? - How do I change the background color of a button within a custom UITableViewCell programatically in Swift? 如何以编程方式更改标签栏的背景颜色? - How to change the background color of tab bar programatically? 如何在 Swift 中以编程方式更改 splitViewController 中的详细视图 - How to change detail View in splitViewController programatically in Swift 如何在Xcode Swift中以编程方式更改UIBarButtunItem功能 - How to change UIBarButtunItem functionality programatically in Xcode swift 如何在 Swift 中更改 UIImage 的颜色 - How to change the color of a UIImage in Swift 如何在Swift中更改UIBezierPath的颜色? - How to change the color of a UIBezierPath in Swift? 如何在 Swift 5 中更改 MKPointAnnotation 的颜色? - How to change the color of a MKPointAnnotation in Swift 5? 如何在 swift 4 中将徽章放在 UIBarButtonItem 上? - how to put badge on UIBarButtonItem in swift 4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM