iOS 11 大标题导航栏不折叠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46373055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:31:09  来源:igfitidea点击:

iOS 11 large-title navigation bar not collapsing

iosobjective-cuikitios11

提问by Tamás Zahola

The Apple guy in the What's new in Cocoa Touch WWDC video saidthat the new large-title navigation bar will magically hook into the top-level scroll view of the underlying view controller and collapse/expand itself automatically while scrolling up and down. (And by "magically", he probably meant that they failed to monkey patch this functionality into the already embarassing UINavigationController-UINavigationBar-UINavigationitemAPIs in a usable way, so they had to resort to hooking into some heuristically chosen scroll view behind the scenes)

苹果公司在 Cocoa Touch WWDC中的新功能视频中表示,新的大标题导航栏会神奇地钩入底层视图控制器的顶级滚动视图,并在上下滚动时自动折叠/展开自身。(这里的“神奇”,他很可能意味着他们没有猴子补丁这个功能进入已经尴尬UINavigationController- UINavigationBar-UINavigationitem在一个可用的方式的API,因此他们不得不求助于挂钩到幕后的一些试探性地选择滚动视图)

Even though I was prepared that this "automatic" collapse/expand wouldn't work if I deviate the slightest from the basic UINavigationController+ UITableView/UICollectionViewsetup, it seems that even in this simplest case it doesn't work as expected.

即使我已经准备好,如果我偏离基本的UINavigationController+ UITableView/UICollectionView设置,这种“自动”折叠/展开将不起作用,但似乎即使在这种最简单的情况下,它也无法按预期工作。

Here's what I have:

这是我所拥有的:

A UITabBarControllerwhich contains a UINavigationController, which contains a UIViewController, which has a UITableViewas its view. Tapping the first cell in the table will push a second view controller on the navigation stack:

AUITabBarController包含 a UINavigationController,其中包含 a UIViewController,其中包含 aUITableView作为其view。点击表格中的第一个单元格将在导航堆栈上推送第二个视图控制器:

storyboard

故事板

No code, just the storyboard.

没有代码,只有故事板。

I've checked "Prefers large titles"for the navigation bar to activate large titles. Now, if I run the app and scroll up/down on the table view, the navigation bar stays the same - large - size; it doesn't collapse:

我已选中导航栏的“首选大标题”以激活大标题。现在,如果我运行应用程序并在表格视图上向上/向下滚动,导航栏将保持不变 - 大尺寸;它不会崩溃:

stuck with large title

被大标题卡住了

However, I've found that if I set the second view controller's navigation item to use the small navigation bar (by setting "Large Title"to the value "Never"), then if I open that page and navigate back, the interactive collapse magically starts working on the first page:

但是,我发现如果我将第二个视图控制器的导航项设置为使用小导航栏(通过将"Large Title"设置为值"Never"),那么如果我打开该页面并导航回来,交互式折叠神奇地开始在第一页上工作:

interactive collapse works after back navigation

返回导航后交互式折叠工作

Am I missing something here, or is this feature not working properly? Here's the sample project I'm using: https://github.com/tzahola/iOS-11-Large-Title-Navigation-Bar

我在这里遗漏了什么,还是这个功能不能正常工作?这是我正在使用的示例项目:https: //github.com/tzahola/iOS-11-Large-Title-Navigation-Bar

And by the way, I'm using the officially released iOS 11, not the betas.

顺便说一下,我使用的是正式发布的 iOS 11,而不是测试版。

2017-09-23 Update: I've sent a bug report to Apple, and opened a ticket on openradar.me: http://www.openradar.me/radar?id=5017601935671296

2017-09-23 更新:我已经向苹果发送了一个错误报告,并在 openradar.me 上开了一张票:http: //www.openradar.me/radar?id=5017601935671296

采纳答案by Tamás Zahola

Good news! I've just figured out that if I set "Large Titles"to "Never"on the storyboard, and then set it via code, then it works:

好消息!我刚刚发现,如果我在情节提要上将“大标题”设置为“从不”,然后通过代码设置它,那么它就可以工作:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAutomatic;
}

Seems like Apple forgot to handle the case when the navigation item has its largeTitleDisplayModeset via the Interface Builder.

当导航项largeTitleDisplayMode通过 Interface Builder 设置时,Apple 似乎忘记处理这种情况。

So until they fix this issue, leave "Large Titles"as "Never"on storyboards, and set them via code in viewDidLoad.

因此,在他们解决此问题之前,将故事板上的“大标题”保留为“从不”,并通过viewDidLoad.

You just need to do that to the first view controller. Subsequent view controllers honor the value in storyboard.

您只需要对第一个视图控制器执行此操作。随后的视图控制器尊重故事板中的值。

回答by kamil3

If there is any other view in addition to tableView, also make sure tableView is on the top of that view(s), right under the Safe Area:

如果除了 tableView 之外还有任何其他视图,还要确保 tableView 位于该视图的顶部,就在安全区域的正下方:

enter image description here

在此处输入图片说明

回答by D4ttatraya

Or instead of changing anything in storyboard, do this:

或者不要更改故事板中的任何内容,而是执行以下操作:

override func viewDidLoad() {
    super.viewDidLoad()
    if #available(iOS 11.0, *) {
        self.navigationItem.largeTitleDisplayMode = .never
        self.navigationItem.largeTitleDisplayMode = .always
    }
}

No matter which language!

不管是什么语言!

This is because large titles on navigation item decides whether or not to collapse on the basis of large title behaviour on previous screen navigation item title.

这是因为导航项上的大标题根据前一屏幕导航项标题的大标题行为决定是否折叠。

回答by Sergio Trejo

Since I can't comment I'll share here the answer I posted

由于我无法发表评论,我将在这里分享我发布的答案

https://stackoverflow.com/a/47493375/8385022

https://stackoverflow.com/a/47493375/8385022

I found a workaround on this site basically, if the tableView (or element that has scroll)is not the first view in your view hierarchy, the large title fails to hide automatically.

我基本上在这个站点上找到了一种解决方法,如果 tableView(或具有滚动的元素)不是视图层次结构中的第一个视图,则大标题无法自动隐藏。

Example that will NOT workExample that will work

行不通的例子行得通的例子

https://markusbodner.com/2017/10/08/fix-large-navigation-bar-title-not-hiding-on-scroll-in-ios-11/

https://markusbodner.com/2017/10/08/fix-large-navigation-bar-title-not-hiding-on-scroll-in-ios-11/

I added on the view willAppear:

我在视图 willAppear 上添加了:

        if #available(iOS 11.0, *) {
        navigationController?.navigationBar.prefersLargeTitles = true
    } else {
        // Fallback on earlier versions
    }

回答by Glenn

Year 2020, iOS 13.0, this WAS NEVER mentioned here. I literally spent an hour or two for this.

2020 年,iOS 13.0,这里从未提及。我真的为此花了一两个小时。

Issue: Large title won't collapse when doing layout programmatically using Snapkit (an autolayout framework)

问题:使用 Snapkit(自动布局框架)以编程方式进行布局时,大标题不会折叠

Solution: SETUP YOUR VIEWS (including navigationController stuff and tableView) inside loadView()NOT in viewDidLoad().

解决方案:在loadView()NOT in 中设置您的视图(包括 navigationController 和 tableView)viewDidLoad()

回答by satish

@TamasZahola @mohamede1945

@TamasZahola @mohamede1945

Guys I had the same problem. I was able to resolve this issue by adding following snippet on my first View Controller of Navigation Controller

伙计们,我遇到了同样的问题。我能够通过在导航控制器的第一个视图控制器上添加以下代码段来解决此问题

navigationController?.navigationBar.prefersLargeTitles = true