gpt4 book ai didi

ios - 导航标题自定义 View 操作未触发

转载 作者:可可西里 更新时间:2023-11-01 02:01:05 26 4
gpt4 key购买 nike

我正在尝试向要添加到导航 titleView 的 View 添加操作

let titleview = UIView()
titleview.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(VC.openPopup))
titleview.addGestureRecognizer(tapGesture)
let label = UILabel()
label.textColor = UIColor.white
label.font = UIFont.systemFont(ofSize: 12)
label.text = "Tap Here to change"
titleview.addSubview(self.selectGroupButton)
titleview.addSubview(label)

我将此 View 添加为导航标题 View

self.navigationItem.titleView = self.selectGroupView
self.navigationItem.titleView?.isUserInteractionEnabled = true

但是我的方法没有被调用。

最佳答案

在你的代码中我可以看到几个问题,

  1. 没有为标签设置框架。
  2. 不确定您是否为按钮设置了选择器

请看下面的代码改动

let titleview = UIView()
titleview.frame = CGRect(x: 0, y: 0, width: 200, height: 40)
titleview.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(your class.openPopup))
titleview.addGestureRecognizer(tapGesture)
let label = UILabel()
label.frame = CGRect(x: 0, y: 0, width: 200, height: 20)
label.textColor = UIColor.red
label.font = UIFont.systemFont(ofSize: 12)
label.text = "Tap Here to change"

let selectGroupButton: UIButton = UIButton(frame: CGRect(x:0, y:20, width:200, height:20))
selectGroupButton.setTitle("tt", for: UIControlState.normal)
selectGroupButton.setTitleColor(UIColor.red, for: UIControlState.normal)
selectGroupButton.addTarget(self, action: #selector(yourclass.methodcall), for: UIControlEvents.touchUpInside)

titleview.addSubview(selectGroupButton)
titleview.addSubview(label)

self.navigationItem.titleView = titleview
self.navigationItem.titleView?.isUserInteractionEnabled = true

关于ios - 导航标题自定义 View 操作未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46297575/

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