【问题标题】:How to change prompt color in Swift 5 iOS16如何在 Swift 5 iOS16 中更改提示颜色
【发布时间】:2022-12-24 16:40:09
【问题描述】:

我正在尝试更改导航控制器中提示的颜色,使其对于 iOS16 为白色而不是黑色。

以下代码更改标题但不更改提示。我的代码是:

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

我需要添加什么来更改提示颜色?谢谢。

【问题讨论】:

  • 这可能是不可能的。多年来,“提示”一直是一纸空文。您不能指望对古老的过时功能发出现代命令。
  • 您可以尝试说self.navigationController?.navigationBar.barStyle = .black,但我真的不希望它起作用。

标签: ios swift swift5 ios16


【解决方案1】:

这似乎是一个错误,我怀疑 Apple 会修复它。

我通过子类化 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
    }
    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 2023-03-18
    • 1970-01-01
    • 2017-12-17
    相关资源
    最近更新 更多