gpt4 book ai didi

ios - Swift 中可点击图像作为导航栏标题

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

我浏览了这里讨论的几个线程:

  1. 设置标题图像
  2. 使用标题按钮

但是,我正在尝试将两者结合起来,即可点击的图像(按钮)作为标题。

不幸的是,虽然下面的代码编译了图像,但在分配为按钮时不会显示图像。有什么建议吗?

func addNavBarImage() {
let navController = navigationController!

let button = UIButton(type: .custom)
var image = UIImage(named: "textLogo")
image = image?.withRenderingMode(.alwaysOriginal)

let bannerWidth = navController.navigationBar.frame.size.width
let bannerHeight = navController.navigationBar.frame.size.height

let bannerX = bannerWidth / 2 - image!.size.width / 2
let bannerY = bannerHeight / 2 - image!.size.height / 2

button.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
button.imageView?.contentMode = .scaleAspectFit
button.imageView?.image = image

button.addTarget(self, action: #selector(self.logoTapped), for: .touchUpInside)
self.navigationItem.titleView = button
}

最佳答案

创建UIImageView,将其添加为titleView并将UITapGestureRecognizer添加到您的UIImageView

let imageView = UIImageView()
imageView.image = UIImage.named("yourImage")
// do any settings to your imageView
let titleTap = UITapGestureRecognizer(target: self, action: #selector(titlePressed))
self.navigationItem.titleView = imageView
self.navigationItem.titleView?.isUserInteractionEnabled = true
self.navigationItem.titleView?.addGestureRecognizer(titleTap)

关于ios - Swift 中可点击图像作为导航栏标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53038871/

27 4 0