简体   繁体   English

如何在 Swift 5 iOS16 中更改提示颜色

[英]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.我正在尝试更改导航控制器中提示的颜色,使其对于 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.这似乎是一个错误,我怀疑 Apple 会修复它。

I've worked around it by subclassing the UINavigationController and diving for the label.我通过子类化 UINavigationController 并深入研究标签来解决这个问题。

@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
    }
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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