Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIAPRecognizer是否未以编程方式在UIView上工作?_Ios_Swift_Firebase - Fatal编程技术网

Ios UIAPRecognizer是否未以编程方式在UIView上工作?

Ios UIAPRecognizer是否未以编程方式在UIView上工作?,ios,swift,firebase,Ios,Swift,Firebase,我很难理解为什么当我的UIView被点击时,我没有调用我的函数。我的UIView在我的setUpNavBar()中我做错了什么?我的viewDidLoad是一个tableView,但我在setUpNavbar中有一个子视图,其中包含我的标题和图像。我首先通过添加titleView.isUserInteractionEnabled=true来解决问题,但事实并非如此 import UIKit import Firebase class MessageController: UITableView

我很难理解为什么当我的UIView被点击时,我没有调用我的函数。我的UIView在我的setUpNavBar()中我做错了什么?我的viewDidLoad是一个tableView,但我在setUpNavbar中有一个子视图,其中包含我的标题和图像。我首先通过添加titleView.isUserInteractionEnabled=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)

    }

您的
profileImageView
正在“吃掉”触摸。尝试禁用其用户交互。它应该允许触摸传递到其父级,
containerView

profileImageView.isUserInteractionEnabled = false

您的
profileImageView
正在“吃掉”触摸。尝试禁用其用户交互。它应该允许触摸传递到其父级,
containerView

profileImageView.isUserInteractionEnabled = false

看起来您缺少标题视图上的高度和宽度约束,因此其大小最终为(0,0),因此没有可触摸区域

尝试设置以下约束,而不是设置框架特性:

    titleView.heightAnchor.constraint(equalToConstant: 40).isActive = true
    titleView.widthAnchor.constraint(equalToConstant: 100).isActive = true

看起来您缺少标题视图上的高度和宽度约束,因此其大小最终为(0,0),因此没有可触摸区域

尝试设置以下约束,而不是设置框架特性:

    titleView.heightAnchor.constraint(equalToConstant: 40).isActive = true
    titleView.widthAnchor.constraint(equalToConstant: 100).isActive = true

您是否尝试过设置self.navigationItem.isUserInteractionEnabled=true?这将转到哪里?在
titleView.isUserInteractionEnabled=true
之前,我收到一个错误。类型为“UINavigation”的值没有成员“isUserInteractionEnabled”您是否尝试过设置
self.navigationItem.isUserInteractionEnabled=true
?这将转到哪里?在
titleView.isUserInteractionEnabled=true
之前,我收到一个错误。“UINavigation”类型的值没有成员“isUserInteractionEnabled”