简体   繁体   中英

UIButton and Labels in Navigation Bar Title View in Storyboard

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:

导航栏视图

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM