导航标题自定义视图操作未触发

时间:2017-09-19 10:03:38

标签: ios swift uinavigationcontroller uinavigationbar

我正在尝试向我添加到导航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)

我将此视图添加为导航标题视图

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

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

3 个答案:

答案 0 :(得分:1)

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

  1. 没有为标签设置框架。
  2. 不确定是否为按钮设置选择器
  3. 请参阅以下代码更改

    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
    

答案 1 :(得分:0)

试试这个:

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 ...

    }

答案 2 :(得分:0)

frame提交给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")
}