2

I am trying to add action to a view which i am adding to navigation 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)

I am adding this view as navigation titleView

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

But my method is not being called.

technerd
  • 14,144
  • 10
  • 61
  • 92
AppleBee
  • 452
  • 3
  • 16

3 Answers3

1

In your code I can see few issues,

  1. Not setting frame for the lable.
  2. Not sure you setup selector for the button

Please see the following code changes

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
0

Try This :

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)

Then Implement Your gesture event like this,

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

    }
0

You are missing with giving frame to titleview

  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")
}
Jack
  • 13,571
  • 6
  • 76
  • 98