【问题标题】:Navigation bar display with UIImage and UILabel for title带有 UIImage 和 UILabel 标题的导航栏显示
【发布时间】:2021-10-25 01:54:06
【问题描述】:

我想用 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")

【问题讨论】:

  • 一个叫 'imageView' 的人是从哪里来的?显示您定义和设置的行。
  • 使用自定义UIView 并在其上布置UIImageViewUILabel,然后将其实例分配给titleView
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: swift uiimage uilabel navigationbar


【解决方案1】:

在导航栏中添加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
    }

}

故事板:

输出:

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 2014-09-08
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多