簡體   English   中英

當在 Xcode 11, beta 2 的 Interface Builder 中指定條形着色顏色時,UITabBarItem 圖標的顏色對於 iOS 13 不正確

[英]UITabBarItem icon not colored correctly for iOS 13 when a bar tint color is specified in Interface Builder in Xcode 11, beta 2

當我使用 Xcode 11, beta 2 在 iOS 13 模擬器上運行時,我的 UITabBarItems 的顏色有問題。我從頭開始制作了一個示例項目,當我沒有指定條形色調顏色時,一切正常。 但是,當我通過 Interface Builder 指定自定義條形色調顏色時,我得到以下信息:

兩個選中的和未選中的項目圖標都有高亮顏色

如果我將 Interface Builder 中的“Bar Tint”屬性設置為不清晰,則選項卡欄中的所有項目圖標都具有選定的顏色。 當它設置為清除時,圖標的顏色正確。 如果我在 iOS 12 模擬器中編譯和運行,圖標也會正確着色。

這似乎是 Xcode 11 中的一個錯誤,但也許我遺漏了什么?

iOS 13 中有一個新的外觀 API。要使用 Xcode 11.0 正確為標簽欄項目的圖標和文本着色,您可以像這樣使用它:

    if #available(iOS 13, *) {
        let appearance = UITabBarAppearance()

        appearance.backgroundColor = .white
        appearance.shadowImage = UIImage()
        appearance.shadowColor = .white

        appearance.stackedLayoutAppearance.normal.iconColor = .black
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
        appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .blue

        appearance.stackedLayoutAppearance.selected.iconColor = .red
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]

        self.tabBar.standardAppearance = appearance

    } 

從表面上看,這似乎是一個錯誤,但是您可以通過在 UITabBar 實例上定義 .unselectedItemTintColor 來緩解它。

self.tabBar.unselectedItemTintColor = [UIColor lightGrayColor];

使用IB中的屬性字段“Image Tint”。

使用IB中的屬性字段“Image Tint”

蘋果自己的Podcasts App也有同樣的問題。 當前是一個錯誤。

對於 Objective C,您可以在 AppDelegate 中使用:

[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:0.65f]];
    if #available(iOS 13.0, *) {
        let appearance = tabBar.standardAppearance
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue]
        appearance.stackedLayoutAppearance.normal.iconColor = UIColor.black
        appearance.stackedLayoutAppearance.selected.iconColor = UIColor.blue
        tabBar.standardAppearance = appearance
    } else {
        tabBar.unselectedItemTintColor = UIColor.black
        tabBar.tintColor = UIColor.blue
    }
self.tabBarController?.tabBar.unselectedItemTintColor = UIColor.lightGray

這在 swift 4 中對我有用。只需將它放在override func viewWillDisappear(_ animated: Bool)方法中,它就會隨着視圖的變化而更新。

所以它看起來像這樣

override func viewWillDisappear(_ animated: Bool) {
    self.tabBarController?.tabBar.unselectedItemTintColor = UIColor.lightGray
}

即使這是一個錯誤(我不確定),您也可以使用它來使用您選擇的任何顏色來更改標簽欄項目的顏色

感謝 Samuël 的回答。 這是我的應用程序中的 UITabBar 設置,已經是 2021 年了,但互聯網上關於如何為 iOS 13 及更高版本設置UITabBar有用信息仍然很少見。

    if #available(iOS 13, *) {
            let appearance = UITabBarAppearance()
            
//            appearance.backgroundColor = .white
            appearance.shadowImage = UIImage()
            appearance.shadowColor = .white
            
            appearance.stackedLayoutAppearance.normal.iconColor = .gray
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.gray]
//            appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .yellow
            
            appearance.stackedLayoutAppearance.selected.iconColor = .systemPink
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.systemPink]
            
            // set padding between tabbar item title and image
            appearance.stackedLayoutAppearance.selected.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
            appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
            
            self.tabBar.standardAppearance = appearance
        } else {
            // set padding between tabbar item title and image
            UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
            UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.gray], for: .normal)
            UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.systemPink], for: .selected)
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM