web-dev-qa-db-ja.com

Swift 4でNavBarタイトルのテキストの色を変更するにはどうすればよいですか?

Swift 3で開発するとき、私は次のように書いていました。

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]

これをAppDelegateに入れると、すべてのUINavbarのタイトルの色がオレンジ色に変わります。

今、私はSwift 4とiOS11で同じことをしたいと思います。

5
Sanf0rd

foregroundColortitleTextAttributesプロパティを設定することにより、UINavigationBarのタイトルの色を変更できます。

文書化されているように ここ

TitleTextAttributesプロパティは、バーのタイトルテキストを表示するための属性を指定します。 font、foregroundColor、およびshadowキーをそれぞれ使用して、テキスト属性ディクショナリのタイトルのフォント、テキストの色、テキストの影の色、およびテキストの影のオフセットをそれぞれ指定できます。

したがって、これを行うと同じ効果があります。

UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.orange]
15
mtrevia

Swift 4.1ナビゲーションバーのタイトルの色の変更

    if #available(iOS 11.0, *) {
        //To change iOS 11 navigationBar largeTitle color

        UINavigationBar.appearance().prefersLargeTitles = true
        UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    } else {
        // for default navigation bar title color
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    }

Swift 4.2+ナビゲーションバーのタイトルの色の変更

    if #available(iOS 11.0, *) {
        //To change iOS 11 navigationBar largeTitle color

        UINavigationBar.appearance().prefersLargeTitles = true
        UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    } else {
        // for default navigation bar title color
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    }
9
Ezimet

スウィフト4

    UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.orange]
4
Ahmed Safadi

アプリデリゲートSwift 4.2

 UINavigationBar.appearance().barTintColor = UIColor.white
 UINavigationBar.appearance().tintColor = UIColor(hex: 0xED6E19)
 UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(hex: 0xED6E19)]
3
CSE 1994

大規模なタイトルと通常のタイトルの両方に対するSwift4の実装:

extension UINavigationController {
    func setTitleForgroundTitleColor(_ color: UIColor) {
        self.navigationBar.titleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): color]
    }

    func setLargeTitleColor(_ color: UIColor) {
        self.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): color]
    }

    func setAllTitleColor(_ color: UIColor) {
        setTitleForgroundTitleColor(color)
        setLargeTitleColor(color)
    }
}
2
thexande

このコードを使用して、ナビゲーションバーのタイトルの色を変更できます

navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
1
Bilal Mustafa

Swift 4.2

fileprivate func convertToOptionalNSAttributedStringKeyDictionary(_ input: [String: Any]?) -> [NSAttributedString.Key: Any]? {
        guard let input = input else { return nil }
        return Dictionary(uniqueKeysWithValues: input.map { key, value in (NSAttributedString.Key(rawValue: key), value)})
}

使用法

   // Set Navigation bar Title color

UINavigationBar.appearance().titleTextAttributes = convertToOptionalNSAttributedStringKeyDictionary([NSAttributedString.Key.foregroundColor.rawValue : UIColor.white])
0
Durul Dalkanat

Swift 5誰かが私のように周りを見回している場合:

self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(displayP3Red: 84/255, green: 93/255, blue: 118/255, alpha: 1)]