简体   繁体   中英

How to change prompt color in Swift 5 iOS16

I am trying to change the color of the prompt in my navigation controller so that it is white not black for iOS16.

在此处输入图像描述 The following code changes the title but not the prompt. My code is:

import UIKit

class ParentViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = UIColor.blue //UIColor.lincsNavBarBlueColor()
        appearance.titleTextAttributes[NSAttributedString.Key.foregroundColor] = UIColor.white

        navigationItem.standardAppearance = appearance
        navigationItem.scrollEdgeAppearance = appearance

        navigationItem.title = "Hello there"
        navigationItem.prompt = "This is the prompt"
    }
}

What do I need to add to change the prompt color? Thanks.

This seems like a bug and I doubt Apple will fix it.

I've worked around it by subclassing the UINavigationController and diving for the label.

@objc
final class NavigationControllerWithPrompt: UINavigationController {
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        changePromptColor()
    }
    
    private func changePromptColor() {
        let promptView = navigationBar.subviews.first { view in
            return String(describing: type(of: view)) == "_UINavigationBarModernPromptView"
        }
        let promptLabel = promptView?.subviews.compactMap{ $0 as? UILabel }.first
        promptLabel?.textColor = UIColor.white
    }
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM