带有大标题的UINavigationBar-如何在iOS 11中找到额外的高度 [英] UINavigationBar with Large Titles - how to find extra height in iOS 11

查看:95
本文介绍了带有大标题的UINavigationBar-如何在iOS 11中找到额外的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 11 中为 UINavigationController UINavigationBar 使用preferredsLargeTitles时,导航栏会增加高度.在我检查过的iPhone上,从44台增加到96台,但是我认为这些数字可以随每台设备而变化(或者至少我们需要尽可能地编码).

When using prefersLargeTitles for a UINavigationController's UINavigationBar in iOS 11, the nav bar increases height. The increase is from 44 to 96 on the iPhones I have checked, but I think those numbers can change per device (or at least we need to code as if they can).

我想以编程方式找到额外"高度-当显示大标题时,在传统 UINavigationBar 下添加的大标题区域的高度.我可以轻松找到显示有大标题的栏的整个高度,但是有没有办法以编程方式单独找到栏的大标题部分的高度(没有任何硬编码)?

I want to programmatically find the 'extra' height - the height of the large titles area that is added beneath the traditional UINavigationBar when a large title is displayed. I can easily find the entire height of the bar with the large title displayed, but is there a way to programmatically find the height of the large title portion of the bar alone (without any hardcoding)?

我需要这样做的原因是,有些时候我想以编程方式滚动到UITableView的顶部,向下拉大标题(已在常规高度"导航栏下方向上滚动),以便显示,而我需要的内容偏移量是导航栏的额外高度.我可以使用导航栏的总高度,但这会把UITableView拉得太远.为此,我需要按如下所示进行硬编码:

The reason I need this is that there are times that I want to programmatically scroll to the top of a UITableView, pulling down the large title (which has scrolled up under the "regular height" nav bar) so that it is showing, and the content offset I need is the extra height of the nav bar. I could use the total height of the navigation bar, but this would pull the UITableView down too far. To do this now, I need to hardcode as below:

[self.tableView setContentOffset:CGPointMake(0, -52) animated:NO];

推荐答案

我认为您找不到清晰解决问题的方法.您要的是导航栏内部的一部分,该部分不会向用户公开.但是,我想我可以为您提供解决方法.

I think you won't find a way to clearly resolve your problem. What you are asking for is a part of a navigation bar internals which is not exposed to the user. However, I think I can offer you a workaround.

要了解这一点,让我们看一下视图调试器中的导航栏(已启用大标题).如您在下图所见,还有一个额外的视图,其中包含特定视图控制器的大标题.如果您不支持大标题,则不会显示该视图.

To understand this let's take a look at the navigation bar (with large titles enabled) in the view debugger. As you can see on the below image there is an extra view containing the large title for a particular view controller. This view is not present when you don't support large titles.

基本上,您想知道的是该视图的高度.

Basically, what you want to know is the height of that view.

这是我建议的解决方案/解决方法

extension UINavigationBar
{
    var largeTitleHeight: CGFloat {
        let maxSize = self.subviews
            .filter { $0.frame.origin.y > 0 }
            .max { $0.frame.origin.y < $1.frame.origin.y }
            .map { $0.frame.size }
        return maxSize?.height ?? 0
    }
}

功能

  1. 过滤掉从导航栏顶部开始的子视图.它们很可能是背景或导航区域,我们不需要它们.
  2. 在导航栏中找到最低的子视图.
  3. 将生成的子视图框架映射到框架大小.
  4. 返回找到的子视图的高度;如果未找到,则返回0.

此方法的明显缺点是,它与导航栏的结构紧密耦合,将来可能会发生变化.但是,您不可能获得比一个或另一个肮脏的把戏更多的东西.

The obvious drawback of this approach is that it's tightly coupled to the structure of the navigation bar which may change in the future. However, it's unlikely that you will get something more than one or another dirty trick.

执行结果应如下所示.

print("Extra height: \(navigationController!.navigationBar.lagreTitleHeight)")
Extra height: 52.0

这篇关于带有大标题的UINavigationBar-如何在iOS 11中找到额外的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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