1

I have added a couple labels and an image to my navigation title bar. That all works great. But I want to cover all of it with a UIButton, but I can't get the UIButton to register any taps.

Here's what my view hierarchy looks like:

Nav Bar Views

User interaction is enabled on everything, and I have an IBAction connected to the Button like this:

@IBAction func tapProjectEdit(_ sender: UIButton) {
  print("This never fires. :(")
}

Am I not allowed to have a UIButton in the navigation bar? Is there another way to pull this off?

Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128

1 Answers1

5

I finally figured this out. I had a suspicion that the UIButton (and everything else in the View) didn't have an actual size since navigation subviews always act a little funny.

So I subclassed UIView, set that class to the View, and added this:

class FancyTitleView: UIView{
  override var intrinsicContentSize: CGSize {
    return UILayoutFittingExpandedSize
  }
}

Now it's working with a regular IBAction (no gesture recognizer required).

This post helped me discover this: custom titleView of navigationItem is not getting tapped on iOS 11

Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128