首页 > 解决方案 > UITapRecognizer 不能以编程方式在 UIView 上工作?

问题描述

我无法理解为什么当我的 UIView 被点击时,我没有调用我的函数。我的 UIView 在我的 setUpNavBar() 我做错了什么?我的 viewDidLoad 是一个 tableView,但我在 setUpNavbar 中有一个子视图,其中包含我的标题和图像。我首先通过添加titleView.isUserInterationEnabled = true将成为解决方案,但事实并非如此。

import UIKit
import Firebase

class MessageController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()



        var ref: DatabaseReference!
        ref = Database.database().reference()


        let userID = Auth.auth().currentUser?.uid
        ref.child("users/profile").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in

            print(snapshot)
            if let dictionary = snapshot.value as? [String: AnyObject] {
//            self.navigationItem.title = dictionary["username"] as? String

                let user = User()
                user.setValuesForKeys(dictionary)
                self.setUpNavBar(user: user)

            }
        }, withCancel: nil)

    }

    func setUpNavBar(user: User) {
        let titleView = UIView()

        titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
        titleView.backgroundColor = UIColor.red

        let containerView = UIView()
        containerView.translatesAutoresizingMaskIntoConstraints = false
        titleView.addSubview(containerView)

        let profileImageView = UIImageView()
        profileImageView.translatesAutoresizingMaskIntoConstraints = false
        profileImageView.contentMode = .scaleAspectFill
        profileImageView.layer.cornerRadius = 20
        profileImageView.clipsToBounds = true
        if let profileImageUrl = user.photoURL {
            profileImageView.loadImageUsingCacheWithURLString(urlString: profileImageUrl)
        }
        containerView.addSubview(profileImageView)

        profileImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
        profileImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
        profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true


        let nameLabel = UILabel()

        containerView.addSubview(nameLabel)
        nameLabel.text = user.username
        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: containerView.rightAnchor).isActive = true
        nameLabel.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = true

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

        self.navigationItem.titleView = titleView

        titleView.isUserInteractionEnabled = true

        let mytapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showChatController))
        mytapGestureRecognizer.numberOfTapsRequired = 1
        titleView.addGestureRecognizer(mytapGestureRecognizer)
    }

    @objc func showChatController() {
        print("clicked")
//        let chatLogController = ChatLogController()
//        navigationController?.pushViewController(chatLogController, animated: true)

    }

标签: iosswiftfirebase

解决方案


profileImageView正在“吃掉”这些触摸。尝试禁用它的 userInteraction。它应该允许将触摸传递给它的父级,containerView

profileImageView.isUserInteractionEnabled = false

推荐阅读