Can't change UINavigationBar prompt color

前端 未结 9 1554
-上瘾入骨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:29

    More complicated version to support old and new iOS

        func updatePromptUI(for state: State) {
        if (state != .Online) {
            //workaround for SOFT-7019 (iOS 11 bug - Offline message has transparent background)
            if #available(iOS 11.0, *) {
                showPromptView()
            } else {
                showOldPromptView()
            }
        }
        else {
            self.navigationItem.prompt = nil
            if #available(iOS 11.0, *) {
                self.removePromptView()
            } else {
                self.navigationController?.navigationBar.titleTextAttributes = nil
                self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.lightGray]
            }
        }
    }
    
    private func showOldPromptView() {
        self.navigationItem.prompt = "Network Offline. Some actions may be unavailable."
        let navbarFont = UIFont.systemFont(ofSize: 16)
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: navbarFont, NSAttributedStringKey.foregroundColor:UIColor.white]
    }
    
    private func showPromptView() {
        self.navigationItem.prompt = String()
        self.removePromptView()
    
        let promptView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 18))
        promptView.backgroundColor = .red
        let promptLabel = UILabel(frame: CGRect(x: 0, y: 2, width: promptView.frame.width, height: 14))
        promptLabel.text = "Network Offline. Some actions may be unavailable."
        promptLabel.textColor = .white
        promptLabel.textAlignment = .center
        promptLabel.font = promptLabel.font.withSize(13)
        promptView.addSubview(promptLabel)
        self.navigationController?.navigationBar.addSubview(promptView)
    }
    
    private func removePromptView() {
        for view in self.navigationController?.navigationBar.subviews ?? [] {
            view.removeFromSuperview()
        }
    }
    

提交回复
热议问题