gpt4 book ai didi

iOS navigationItem titleView 图片和文字

转载 作者:行者123 更新时间:2023-11-28 19:33:27 29 4
gpt4 key购买 nike

我想用图像和文本设置 navigationItem 的 titleView,如下图所示:

enter image description here

我该如何实现?

更具体地说,我正在尝试在 Swift 中执行此操作。

最佳答案

条件,你的 Controller 必须有它的navigation controller:

enter image description here

使用我下面写的代码,结果是这样的:

enter image description here

在你的 View Controller 中,试试下面我的代码:

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

}

关于iOS navigationItem titleView 图片和文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41454562/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com