识别点击导航栏标题 [英] Recognize tap on navigation bar title

查看:84
本文介绍了识别点击导航栏标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击导航栏中的标题时,有人可以帮助我识别点击吗?

Could someone please help me recognise a tap when a user taps on the Title in the Navigation Bar?

我想识别此点击,然后为出现的 tableHeaderView 设置动画.可能将TableView向下滑动.

I would like to recognise this tap and then animate the tableHeaderView appearing. Possibly sliding the TableView down.

这样的想法是用户可以选择一个快速选项(从tableViewHeader中)以重新填充TableView.

The idea being that the user can then select a quick option (from the tableViewHeader) to re-populate the TableView.

但是我无法识别任何轻拍.

However I cannot recognise any taps.

我正在使用Swift.

I'm using Swift.

谢谢.

推荐答案

UINavigationBar不公开其内部视图层次结构.没有支持方法来引用显示标题的UILabel.

UINavigationBar does not expose its internal view hierarchy. There is no supported way to get a reference to the UILabel that displays the title.

您可以手动"在其视图层次结构中扎根(通过搜索其subviews),但是由于视图层次结构是私有的,因此在将来的iOS版本中可能会停止工作.

You could root around in its view hierarchy "manually" (by searching through its subviews), but that might stop working in a future iOS release because the view hierarchy is private.

一种解决方法是创建一个UILabel并将其设置为视图控制器的navigationItem.titleView.由您决定是否匹配默认标签的样式,默认标签的样式可能会在不同版本的iOS中更改.

One workaround is to create a UILabel and set it as your view controller's navigationItem.titleView. It's up to you to match the style of the default label, which may change in different versions of iOS.

也就是说,它很容易设置:

That said, it's pretty easy to set up:

override func didMove(toParentViewController parent: UIViewController?) {
    super.didMove(toParentViewController: parent)

    if parent != nil && self.navigationItem.titleView == nil {
        initNavigationItemTitleView()
    }
}

private func initNavigationItemTitleView() {
    let titleView = UILabel()
    titleView.text = "Hello World"
    titleView.font = UIFont(name: "HelveticaNeue-Medium", size: 17)
    let width = titleView.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).width
    titleView.frame = CGRect(origin:CGPoint.zero, size:CGSize(width: width, height: 500))
    self.navigationItem.titleView = titleView

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

@objc private func titleWasTapped() {
    NSLog("Hello, titleWasTapped!")
}

我将标签的大小设置为其自然宽度(使用sizeThatFits:),但将其高度设置为500.导航栏将保持宽度,但将高度缩小到其自身的高度.这样可以最大程度地增加可点击的区域(因为标签的自然高度可能只有〜22点,而条高为44点).

I'm setting the size of the label to its natural width (using sizeThatFits:), but I'm setting its height to 500. The navigation bar will keep the width but shrink the height to the bar's own height. This maximizes the area available for tapping (since the natural height of the label might be only ~22 points but the bar is 44 points high).

这篇关于识别点击导航栏标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆