skip to Main Content

What am i doing wrong? I also tried to subclass UIView and put the ‘button’ (imageview) in that and use that as my headerview but still tapping doesn’t work. Also tried using UITableHeaderFooterView.

lazy var groupDetailsButton: UIImageView = {
    let imageView = UIImageView()
    imageView.isUserInteractionEnabled = true
    let image = UIImage(named: "details") //?.resizedImage(newWidth: 30)
    imageView.image = image!.withRenderingMode(.alwaysTemplate)
    imageView.backgroundColor = .blue
    imageView.tintColor = .white
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.contentMode = .scaleAspectFill
    imageView.addGestureRecognizer(UIGestureRecognizer(target: self, action: #selector(showGroupDetails)))
    return imageView
}()


func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView(frame: CGRect(x: 0, y: 50, width: tableView.frame.width, height: 50))
    headerView.isUserInteractionEnabled = true
    headerView.addSubview(groupDetailsButton)
    headerView.isUserInteractionEnabled = true
    headerView.addConstraintsWithFormat("V:|-10-[v0(30)]", views: groupDetailsButton)
    headerView.addConstraintsWithFormat("H:[v0(30)]|", views: groupDetailsButton)
    return headerView
}


@objc func showGroupDetails(){
    print("SHOW GROUP DETAILS")
    let groupDetailsController = GroupDetailsController()
    groupDetailsController.groupDetails = groupDetails
    groupDetailsController.modalPresentationStyle = .fullScreen
    self.present(groupDetailsController, animated: true, completion: nil)
}
    

2

Answers


  1. Chosen as BEST ANSWER

    This is very silly and i wasted a day trying to figure out what was wrong. but i was using UIGestureRecognizer instead of UITapGestureRecognizer.


  2. You added a UIGestureRecognizer … instead of a UITapGestureRecognizer recognizer.

    Change this line:

    imageView.addGestureRecognizer(UIGestureRecognizer(target: self, action: #selector(showGroupDetails)))
    

    to this:

    imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showGroupDetails)))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search