IT story

iOS 8의 탐색 막대 막대, 색조 및 제목 텍스트 색상

hot-time 2020. 5. 29. 23:48
반응형

iOS 8의 탐색 막대 막대, 색조 및 제목 텍스트 색상


상태 표시 줄의 배경 텍스트는 여전히 검은 색입니다. 색상을 흰색으로 바꾸려면 어떻게합니까?

// io8, swift, Xcode 6.0.1 
override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()]

}

여기에 이미지 설명을 입력하십시오


AppDelegate.swift, application(_:didFinishLaunchingWithOptions:)나는 다음을 넣었다.

UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]

( NSAttributedStringKey대신 Swift 4 이하 사용 NSAttributedString.Key)

titleTextAttributes에 대해 문서다음 과 같이 말합니다.

텍스트 속성 사전에서 제목의 글꼴, 텍스트 색상, 텍스트 그림자 색상 및 텍스트 그림자 오프셋을 지정할 수 있습니다


나는 Alex의 대답을 좋아한다. 무언가를 빨리 시험 해보고 싶다면 ViewController반드시 사용하십시오.

viewWillAppear()
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    var nav = self.navigationController?.navigationBar
    nav?.barStyle = UIBarStyle.Black
    nav?.tintColor = UIColor.white
    nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
    //nav?.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.orange] // swift 4.2
}

여기에 이미지 설명을 입력하십시오


색상을 보편적으로 변경하려면이 코드는 NavigationControllerviewDidLoad함수 에 있어야 합니다.

class NavigationController: UINavigationController, UIViewControllerTransitioningDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Status bar white font
        self.navigationBar.barStyle = UIBarStyle.Black
        self.navigationBar.tintColor = UIColor.whiteColor()
    }
}

당을 변경하려면, ViewController당신은 참조 할 것 NavigationController으로부터 ViewController한다는 점에서 유사한 라인을 쓰기 ViewControllerviewWillAppear기능.


// 스위프트 4에서

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

objective-c 에서 작업하려면 viewWillAppearCustomViewController에 다음 줄을 넣어야합니다 .

[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setTranslucent:NO];

Swift2.x의 경우 다음 같이 작동합니다.

self.navigationController?.navigationBar.barTintColor = UIColor.redColor()

Swift3.x의 경우 다음 같이 작동합니다.

self.navigationController?.navigationBar.barTintColor = UIColor.red

스토리 보드에서이 작업을 수행하려면 (Interface Builder Inspector)

의 도움으로 IBDesignableInterface Builder Inspector에 더 많은 옵션을 추가 UINavigationController하고 스토리 보드에서 조정할 수 있습니다. 먼저 다음 코드를 프로젝트에 추가하십시오.

@IBDesignable extension UINavigationController {
    @IBInspectable var barTintColor: UIColor? {
        set {
            navigationBar.barTintColor = newValue
        }
        get {
            guard  let color = navigationBar.barTintColor else { return nil }
            return color
        }
    }

    @IBInspectable var tintColor: UIColor? {
        set {
            navigationBar.tintColor = newValue
        }
        get {
            guard  let color = navigationBar.tintColor else { return nil }
            return color
        }
    }

    @IBInspectable var titleColor: UIColor? {
        set {
            guard let color = newValue else { return }
            navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: color]
        }
        get {
            return navigationBar.titleTextAttributes?["NSForegroundColorAttributeName"] as? UIColor
        }
    }
}

그런 다음 스토리 보드에서 UINavigationController의 속성을 설정하십시오.

여기에 이미지 설명을 입력하십시오


전체 앱에 색조 색상과 막대 색상을 설정하려면 AppDelegate.swift에 다음 코드를 추가 할 수 있습니다.

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

    var navigationBarAppearace = UINavigationBar.appearance()

    navigationBarAppearace.tintColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0)
    navigationBarAppearace.barTintColor = UIColor(red:0.76, green:0.40, blue:0.40, alpha:1.0)
    navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    return true
`

탐색 막대 TintColor 및 tintColor가 설정되었습니다.


Swift 4로 업데이트

override func viewDidLoad() {
    super.viewDidLoad()
        self.navigationController?.navigationBar.tintColor = UIColor.blue
        self.navigationController?.navigationBar.barStyle = UIBarStyle.black
}

Swift5 및 Xcode 10에서

self.navigationItem.title = "your name"
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes

스위프트 4

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.barTintColor = UIColor.orange
    navigationController?.navigationBar.tintColor = UIColor.white
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
}

스위프트 4.1

viewDidLoad에 func 추가

override func viewDidLoad() {
  super.viewDidLoad()

  setup()
}   

에서 setup()기능 추가 :

func setup() {

        navigationController?.navigationBar.prefersLargeTitles = true
        navigationController?.navigationBar.barStyle = .blackOpaque
        navigationItem.title = "YOUR_TITLE_HERE"
        navigationController?.navigationBar.barTintColor = .black
        let attributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        navigationController?.navigationBar.largeTitleTextAttributes = attributes
    }

Albert의 답변의 Swift 4.2 버전

UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : UIColor.white]

Swift 버전 4.2에서 탐색 줄 제목의 텍스트 색상을 흰색으로 설정 :

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

사용자 정의 색상을 TitleTextat NavigationBar로 설정하려면 Swift 3의 간단하고 짧은 코드입니다.

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

또는

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

스위프트 4.2

var nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.white
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]

Swift 3에서는 다음과 같이 작동합니다.

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

Swift 3.2를 통해 Swift (Swift 4.0이 아님)

    self.navigationController?.navigationItem.largeTitleDisplayMode = .always
    self.navigationController?.navigationBar.prefersLargeTitles = true
    self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

    // unconfirmed but I assume this works:
    self.navigationController?.navigationBar.barTintColor = UIColor.white
    self.navigationController?.navigationBar.barStyle = UIBarStyle.black

참고 URL : https://stackoverflow.com/questions/26008536/navigationbar-bar-tint-and-title-text-color-in-ios-8

반응형