Swift 中的可点击图像作为导航栏标题
Clickable Image as Nav Bar Title in Swift
我浏览了这里讨论的几个主题:
- 设置标题图片
- 为标题使用按钮
但是,我试图将两者结合起来,即,将可点击的图像(按钮)作为标题。
不幸的是,虽然下面的代码编译图像分配为按钮时不显示。有什么建议吗?
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)
我的工作代码。
let button = UIButton(type: .custom)
button.setImage(UIImage (named: "your_image"), for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
button.backgroundColor = UIColor.clear
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(self.clickOnButton), for: .touchUpInside)
self.navigationItem.titleView = button
这里可以看到最后一行的button直接设置为navigationItem的titleView,这样就会在导航栏中间添加button
按钮的操作方法如下:
@objc func clickOnButton(button: UIButton) {
print("Title Tapped")
}
我浏览了这里讨论的几个主题:
- 设置标题图片
- 为标题使用按钮
但是,我试图将两者结合起来,即,将可点击的图像(按钮)作为标题。
不幸的是,虽然下面的代码编译图像分配为按钮时不显示。有什么建议吗?
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)
我的工作代码。
let button = UIButton(type: .custom)
button.setImage(UIImage (named: "your_image"), for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
button.backgroundColor = UIColor.clear
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(self.clickOnButton), for: .touchUpInside)
self.navigationItem.titleView = button
这里可以看到最后一行的button直接设置为navigationItem的titleView,这样就会在导航栏中间添加button
按钮的操作方法如下:
@objc func clickOnButton(button: UIButton) {
print("Title Tapped")
}