简体   繁体   English

情节提要中导航栏标题视图中的UIButton和标签

[英]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. 但是我想用UIButton覆盖所有内容,但是我无法让UIButton注册任何水龙头。

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连接到Button

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

Am I not allowed to have a UIButton in the navigation bar? 不允许我在导航栏中使用UIButton吗? 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. 我怀疑UIButton (以及View中的其他所有内容)没有实际大小,因为导航子视图总是表现得有些滑稽。

So I subclassed UIView , set that class to the View , and added this: 因此,我UIView子类,将该类设置为View ,并添加以下内容:

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

Now it's working with a regular IBAction (no gesture recognizer required). 现在,它可以与常规IBAction (无需姿势识别器)一起使用。

This post helped me discover this: custom titleView of navigationItem is not getting tapped on iOS 11 这篇文章帮助我发现了这一点: iOS 11上没有利用自定义titleNavigationItem的标题视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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