program story

Swift의 UINavigationBar 텍스트 색상

inputbox 2020. 11. 29. 10:24
반응형

Swift의 UINavigationBar 텍스트 색상


UINavigationBarSwift에서 의 색상을 변경하려면 어떻게해야 합니까?

온라인에서는 대부분 다음과 같이 말합니다.

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

내가 번역 한

let titleDict: NSDictionary = ["NSForegroundColorAttributeName": UIColor.whiteColor()]
self.navigationController.navigationBartitleTextAttributes = titleDict
//self is referring to a UIViewController

하지만 작동하지 않습니다. 이미 배경색과 버튼 색상을 변경했지만 텍스트 색상은 변경되지 않습니다. 어떤 아이디어?


문자열이 NSForegroundColorAttributeName아닌 키로 사용하십시오 "NSForegroundColorAttributeName".

let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.navigationController.navigationBar.titleTextAttributes = titleDict

파일 UINavigationController내에서 앱의 모든 모양을 변경할 수도 있습니다 AppDelegate.swift. application:didFinishLaunchingWithOptions함수 내에 다음 코드를 넣으십시오 .

var navigationBarAppearace = UINavigationBar.appearance()

navigationBarAppearace.tintColor = UIColor.YourNavigationButtonsColor()  // Back buttons and such
navigationBarAppearace.barTintColor = UIColor.YourBackgroundColor()  // Bar's background color

navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.YourTitleColor()]  // Title's text color

Creds : Coderwall의 블로그 게시물


Swift 3+

self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]

스위프트 4.0

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]

스위프트 2.0

self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]

    //Nav Bar Title
    self.title = "WORK ORDER"
    self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]

스위프트 3

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .selected)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.black], for: .normal)

나는 다음과 같이 사용합니다.

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
      let navigationBarAppearace = UINavigationBar.appearance()
      navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]

     return true
   }

Swift 4.x :

UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

스위프트 4.2

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

    let titleDict = [NSForegroundColorAttributeName: UIColor.white]
    self.navigationController?.navigationBar.titleTextAttributes = titleDict

Swift 5.1 :

    let titleDict: NSDictionary = [NSAttributedString.Key.foregroundColor: UIColor.white]
    navigationController?.navigationBar.titleTextAttributes = titleDict as? [NSAttributedString.Key : Any]

참고 URL : https://stackoverflow.com/questions/24402000/uinavigationbar-text-color-in-swift

반응형