UITapGestureRecognizer第一次工作,但第二次不工作

时间:2019-07-11 23:21:59

标签: swift uitapgesturerecognizer

[大大简化]

我正在关注Firebase 3上的“让我们构建那个应用程序” YouTube系列视频,那是从2016年开始的,所以自从Swift演变以来,我不得不重新编写一些代码,但是大部分都是有用的教程。 / p>

但是,我被卡住了。

enter image description here

该红色框旨在作为具有图像和名称的自定义titleView,但我已对其进行了简化以尝试发现问题。

viewWillAppear调用setupNavbar来建立navbar.titleView

func setupNavbar() {
  let titleView = UIView()
  titleView.frame = CGRect(x: 0, y: 0, width: 300, height: 40)
  titleView.backgroundColor = .red

  let containerView = UIView() // for the Image and Label, later

  containerView.translatesAutoresizingMaskIntoConstraints = false
  containerView.backgroundColor = .green

  // left, top, width, height anchors equal to same for titleView
  containerView.leftAnchor.constraint(equalTo: titleView.leftAnchor)
  // top, width, height are similar

  titleView.addSubview(containerView)

  self.navigationItem.titleView = titleView

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

选择器的功能是:

@objc func showChatController() {
  let chatController = ChatLogTableViewController()
  navigationController?.pushViewController(chatController, animated: true)
}

ChatLogTableViewController仅具有默认的viewDidLoad()

首先,我很惊讶该框是红色而不是绿色。为什么会这样?

第二,如果我单击红色框,则将按预期加载ChatController。但是,当我单击“返回”并返回此屏幕时,红色框不可见,因此无法再点击它。但是...。如果我注销并再次登录/登录,则该框为红色,可以再次单击它。

我想念什么?

更新1:“ +”创建一个新的控制器并显示它。

present(UINavigationController(
  rootViewController: NewMessageTableViewController()), 
  animated: true, 
  completion: nil)

该控制器当前为空,除了leftBarButtonItem只是barButtonSystemItem(.cancel)。就像“退出”一样,它也可以“重置”手势,并且可以正常工作。

更新2:根据要求添加了代码。

更新3:问题已大大简化。另外,如果我将showChatController代码更改为print ("show Chat Controller"),则可以点击一下内容。红色框及其点击手势都保留。

因此,当我...pushViewController然后又回来时,发生了一些事情。

4 个答案:

答案 0 :(得分:0)

您是否尝试设置标签的isUserInteractionEnabled属性,该标签设置为navBar的titleView?我知道这很愚蠢,但是我已经错过了很多次了:)

let recognizer = UITapGestureRecognizer(target: self, action: #selector(YourViewController.titleWasTapped))
titleView.isUserInteractionEnabled = true
titleView.addGestureRecognizer(recognizer)

答案 1 :(得分:0)

you can try following things:
1. "User Interaction Enabled" in the UILabel Attributes Inspector in the Storyboard.

2. override func viewDidLoad(animated: Bool) {
    super.viewDidAppear(animated)
    self.titleView.userInteractionEnabled = true
    var tapGesture = titleView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showChatController)))

        self.titleView.addGestureRecognizer(tapGesture)
    }

    func showChatController() {
        println("Tap Gesture recognized")
    }

答案 2 :(得分:0)

请首先做两件事titleview.userInteractionEnabled = true。 第二个为您的titleview和contentview提供不同的颜色,然后查看您何时回来,您的内容视图的名称仍保留在titleview内(因为titleview的宽度仅为100),可能是单击个人资料图像或在最左侧的titleview上都可以使用。我想发表评论,但我没有足够的声誉发表评论。

答案 3 :(得分:0)

您好,@ Zonker,我已经测试了您的setupNavBar()。当我按下并返回单击 titleView 时,代码正常工作了。无法获得 .green 颜色的原因是必须为 containerView 指定高度和宽度,而主要原因是您已放置 addSubView anchor 下面的代码,并且您的.isActive = true中没有containerView.leftAnchor.constraint...,请检查下面的setupNavBar()代码:

func setupNavbar() {
    let titleView = UIView()
    titleView.frame = CGRect(x: 0, y: 0, width: 300, height: 40)
    titleView.backgroundColor = .red

    let containerView = UIView() // for the Image and Label, later

    containerView.translatesAutoresizingMaskIntoConstraints = false
    containerView.backgroundColor = .green
    titleView.addSubview(containerView)
    // left, top, width, height anchors equal to same for titleView
    containerView.widthAnchor.constraint(equalToConstant: 300).isActive = true
    containerView.heightAnchor.constraint(equalToConstant: 40).isActive = true
    containerView.leftAnchor.constraint(equalTo: titleView.leftAnchor).isActive = true
    // top, width, height are similar

    self.navigationItem.titleView = titleView

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

最好在github中推送代码,以便我们测试代码

相关问题