iOS导航项标题查看图像和文本

iOS导航项标题查看图像和文本,ios,uinavigationcontroller,navigationitem,Ios,Uinavigationcontroller,Navigationitem,我想使用图像和文本设置navigationItem的标题视图,如下图所示: 我如何做到这一点 更具体地说,我尝试在Swift条件下执行此操作,您的控制器必须具有其导航控制器: 使用我在下面编写的代码,结果如下: 在视图控制器中,请尝试下面的代码: override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from

我想使用图像和文本设置navigationItem的标题视图,如下图所示:

我如何做到这一点


更具体地说,我尝试在
Swift
条件下执行此操作,您的控制器必须具有其
导航控制器

使用我在下面编写的代码,结果如下:

在视图控制器中,请尝试下面的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    initUI()
}

// MARK: - init

func initUI() {

    let rect:CGRect = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: 64, height: 64))

    let titleView:UIView = UIView.init(frame: rect)
    /* image */
    let image:UIImage = UIImage.init(named: "01.jpg")!
    let image_view:UIImageView = UIImageView.init(image: image)
    image_view.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
    image_view.center = CGPoint.init(x: titleView.center.x, y: titleView.center.y - 10)
    image_view.layer.cornerRadius = image_view.bounds.size.width / 2.0
    image_view.layer.masksToBounds = true
    titleView.addSubview(image_view)

    /* label */
    let label:UILabel = UILabel.init(frame: CGRect.init(x: 0, y: 30, width: 64, height: 24))
    label.text = "Hello"
    label.font = UIFont.systemFont(ofSize: 12)
    label.textAlignment = .center
    titleView.addSubview(label)

    self.navigationItem.titleView = titleView

}

在这种情况下,您的控制器必须具有其
导航控制器

使用我在下面编写的代码,结果如下:

在视图控制器中,请尝试下面的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    initUI()
}

// MARK: - init

func initUI() {

    let rect:CGRect = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: 64, height: 64))

    let titleView:UIView = UIView.init(frame: rect)
    /* image */
    let image:UIImage = UIImage.init(named: "01.jpg")!
    let image_view:UIImageView = UIImageView.init(image: image)
    image_view.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
    image_view.center = CGPoint.init(x: titleView.center.x, y: titleView.center.y - 10)
    image_view.layer.cornerRadius = image_view.bounds.size.width / 2.0
    image_view.layer.masksToBounds = true
    titleView.addSubview(image_view)

    /* label */
    let label:UILabel = UILabel.init(frame: CGRect.init(x: 0, y: 30, width: 64, height: 24))
    label.text = "Hello"
    label.font = UIFont.systemFont(ofSize: 12)
    label.textAlignment = .center
    titleView.addSubview(label)

    self.navigationItem.titleView = titleView

}

@iYoung可能重复,但我还想在图像下方包含文本什么是重复?你看过帖子内容了吗?可能是@iYoung的副本,但我还想在图片下方包含文本什么是副本?你看过帖子的内容了吗?非常感谢!非常感谢你!