标题为 UIImage 和 UILabel 的导航栏显示

Navigation bar display with UIImage and UILabel for title

我想用 UIImage 和 UILabel 放置导航栏标题。

let image = UIImage(systemName: "flame")
imageView.tintColor = R.color.fire()
self.navigationItem.titleView = imageView
self.navigationItem.title = "표시한 제품"
self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: R.color.myPageMenu]

这是我的代码,但此代码仅显示 UIImage(systemName: "flame")

这是在导航栏中添加 UIImageViewUILabel 的方法。

文件:ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var navView: UIView!
    @IBOutlet weak var navTitle: UILabel!
    @IBOutlet weak var navImage: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavTitleView()
    }

    func setNavTitleView() {
        navTitle.text = "표시한 제품"
        navTitle.textColor = .lightGray
        navImage.image = UIImage(systemName: "flame")
        navImage.tintColor = .red
        navigationController?.navigationBar.backgroundColor = .black
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.lightGray]
        navigationController?.navigationBar.tintColor = .lightGray
        navigationItem.titleView = navView
    }

}

故事板:

输出:

let image = UIImage(systemName: "flame")
let title = "표시한 제품"

let imageView = UIImageView()
imageView.image = image

let textLabel = UILabel()
textLabel.text  = text
textLabel.textAlignment = .center

//Stack View
let stackView   = UIStackView()
stackView.axis  = NSLayoutConstraint.Axis.horizontal
stackView.distribution  = UIStackView.Distribution.equalSpacing
stackView.alignment = UIStackView.Alignment.center
stackView.spacing   = 4.0

stackView.addArrangedSubview(imageView)
stackView.addArrangedSubview(textLabel)

self.navigationItem.titleView = stackView