add gesture recognizer uivew navigation bar swift

2019-08-21 03:35发布

问题:

I have a navigation controller with a navigation bar, I have added a UIView with subview of imageView and UILabel for titleView. I need to be able to click on that view to do something else with addGestureRecognizer on that view but nothing is printed on the console. The UIImageView has to be next to the UILabel

Here is the code I tried so far

private func setupNavBarWithUser() {
    let titleView = UIView()
    let width = titleView.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).width
    titleView.frame = CGRect(origin:CGPoint.zero, size:CGSize(width: width, height: 500))

    let profileImageView = UIImageView()
    profileImageView.translatesAutoresizingMaskIntoConstraints = false
    profileImageView.contentMode = .scaleAspectFill
    profileImageView.layer.cornerRadius = 20
    profileImageView.clipsToBounds = true
    ImageService.getImage(withURL: NSURL(string: (user?.pictureURL)!)! as URL) { (image) in
        profileImageView.image = image
    }
    titleView.addSubview(profileImageView)
    profileImageView.leftAnchor.constraint(equalTo: titleView.leftAnchor).isActive = true
    profileImageView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true
    profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
    profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true

    let nameLabel = UILabel()
    titleView.addSubview(nameLabel)
    nameLabel.text = user?.first_name
    nameLabel.textColor = .white
    nameLabel.translatesAutoresizingMaskIntoConstraints = false
    nameLabel.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
    nameLabel.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true
    nameLabel.rightAnchor.constraint(equalTo: titleView.rightAnchor).isActive = true
    nameLabel.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = true

    titleView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
    titleView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true

    self.navigationItem.titleView = titleView
    let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.testing))
    titleView.isUserInteractionEnabled = true
    titleView.addGestureRecognizer(recognizer)
}

@objc func testing() {
    print("hello")
}

Hope someone can help me with this problem, much thank you !!

UPDATE this is where I need to add a gesture recognizer

回答1:

Add this method in viewDidLoad/viewWillAppear

 if let navigationBar = self.navigationController?.navigationBar {
        // create a button
         let button =  UIButton(type: .custom)
        button.frame = CGRect(x: navigationBar.frame.width/2-20, y: 0, width: 100, height: 40)
        button.setTitleColor(.red, for: .normal)
         button.setTitle("Button", for: .normal)
        button.addTarget(self, action: #selector(self.testing), for: .touchUpInside)
        // create a imageview
        let profileImageView = UIImageView()
         profileImageView.contentMode = .scaleAspectFill
        profileImageView.layer.cornerRadius = 20
        profileImageView.clipsToBounds = true
         profileImageView.frame = CGRect(x: navigationBar.frame.width/2-60, y: 0, width: 40, height: 40)
        profileImageView.image = UIImage(named: "heart")
        // Add two elements to navigationbar
        navigationBar.addSubview(profileImageView)
        navigationBar.addSubview(button)

    }