导航标题自定义视图动作不触发

问题描述:

我想补充行动,对此我加入到导航titleview的视图导航标题自定义视图动作不触发

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) 

我加入这个视图导航titleview的

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

但我的方法没有被调用。

+0

您可以在故事板的视图 - 控制标题设计时导航栏上添加的UIView。 –

在你的代码中,我可以看到一些问题,

  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 

试试这个:

let NavigationView = UIView() 
     NavigationView.translatesAutoresizingMaskIntoConstraints = false 
       self.navigationController?.navigationBar.addSubview(NavigationView) 
     self.navigationController?.navigationBar.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[NavigationView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["NavigationView":NavigationView])) 
     self.navigationController?.navigationBar.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[NavigationView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["NavigationView":NavigationView])) 
     NavigationView.backgroundColor = UIColor.orange 
     let singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTapAction)) 
     singleTap.delegate = self 
     singleTap.numberOfTapsRequired = 1 
     NavigationView.addGestureRecognizer(singleTap) 

然后实现你这样的手势事件,

func singleTapAction() { 
     print("Single") 
// Write your code here ... 

    } 

你缺少与给frametitleview

override func viewDidLoad() { 
    super.viewDidLoad() 

    let titleview = UIView(frame: CGRect(x: 20, y: 10, width: 200, height: 24)) 

    titleview.isUserInteractionEnabled = true 
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(openPopup)) 
    titleview.addGestureRecognizer(tapGesture) 
    let label = UILabel(frame: CGRect(x: 40, y: 10, width: 200, height: 24)) 
    label.textColor = .red 
    label.textAlignment = .center 
    label.text = "Tap Here to change" 
    titleview.addSubview(label) 
    self.navigationItem.titleView = titleview 
    self.navigationItem.titleView?.isUserInteractionEnabled = true 

} 
func openPopup(){ 
    print("Jack") 
}