Can't change UINavigationBar prompt color

前端 未结 9 1561
-上瘾入骨i
-上瘾入骨i 2021-01-04 01:57

I am unable to change the prompt color on my navigation bar. I\'ve tried the code below in viewDidLoad, but nothing happens.

self.navigationCont         


        
9条回答
  •  悲&欢浪女
    2021-01-04 02:05

    I've found next work around for iOS 11. You need set at viewDidLoad navigationItem.prompt = UINavigationController.fakeUniqueText and after that put next thing

        navigationController?.promptLabel(completion: { label in
        label?.textColor = .white
        label?.font = Font.regularFont(size: .p12)
            })
    
        extension UINavigationController {
        public static let fakeUniqueText = "\n\n\n\n\n"
        func promptLabel(completion: @escaping (UILabel?) -> Void) {
            gloabalThread(after: 0.5) { [weak self] in
                guard let `self` = self else {
                    return
                }
                let label = self.findPromptLabel(at: self.navigationBar)
                mainThread {
                    completion(label)
                }
            }
        }
    
        func findPromptLabel(at view: UIView) -> UILabel? {
            if let label = view as? UILabel {
                if label.text == UINavigationController.fakeUniqueText {
                    return label
                }
            }
            var label: UILabel?
            view.subviews.forEach { subview in
                if let promptLabel = findPromptLabel(at: subview) {
                    label = promptLabel
                }
            }
            return label
        }
        }
    
    
        public func mainThread(_ completion: @escaping SimpleCompletion) {
            DispatchQueue.main.async(execute: completion)
        }
    
    
        public func gloabalThread(after: Double, completion: @escaping SimpleCompletion) {
           DispatchQueue.global().asyncAfter(deadline: .now() + after) {
           completion()
        }
    }

提交回复
热议问题