swift 中隐藏导航栏

Since everyone wants their app to look neat, attractive, and fascinating, non-standard UI elements and animations are becoming very popular. HidingNavigationBar is such a framework. It helps hide navigation tools while scrolling and unhide them when you stop.

由于每个人都希望他们的应用看起来整洁,吸引人并且引人入胜,因此非标准的UI元素和动画正变得非常流行。 HidingNavigationBar是这样的框架。 它有助于在滚动时隐藏导航工具,并在停止时取消隐藏它们。

In this article, I will explain how to use the framework and why it seems to be the best option to choose.

在本文中,我将解释如何使用该框架以及为什么它似乎是最佳选择。

开始吧 (Let’s Start)

According to the documentation:

根据文档 :

“HidingNavigationBar supports hiding/showing of the following view elements:

“ HidingNavigationBar支持隐藏/显示以下视图元素:

- UINavigationBar

-UINavigationBar

- UINavigationBar and an extension UIView

-UINavigationBar和扩展UIView

- UINavigationBar and a UIToolbar

-UINavigationBar和UIToolbar

- UINavigationBar and a UITabBar”

-UINavigationBar和UITabBar”

Note: HidingNavigationBar only works with UINavigationBars that have translucent set to true.

注意:HidingNavigationBar仅适用于将半透明设置为 true UINavigationBar 。

安装 (Installation)

Basically, there are two ways of installing HidingNavigationBar:

基本上,有两种安装HidingNavigationBar的方法:

  1. With CocoaPods

    与CocoaPods

  2. With Carthage

    与迦太基

Which dependency manager to use is entirely up to you. You can learn more about dependency managers in this article.

使用哪个依赖项管理器完全取决于您。 您可以在本文中了解有关依赖项管理器的更多信息。

After the integration of HidingNavigationBar, you may face some issues. Feel free to scroll directly down to the problem resolution section.

集成HidingNavigationBar之后,您可能会遇到一些问题。 随意直接向下滚动到问题解决部分。

用法 (Usage)

The main feature of HidingNavigationBar is that it should be linked to something that has a scrollable effect. It can be either a UITableView or UIScrollView. You will see that later, so don’t worry.

HidingNavigationBar的主要功能是它应该链接到具有可滚动效果的对象。 它可以是UITableView或UIScrollView 。 稍后您会看到的,所以不用担心。

First, import HidingNavigationBar to UIViewController.

首先, import HidingNavigationBar UIViewController 。

Second, declare a member variable of type HidingNavigationBarManager in your UIViewController subclass.

其次,在您的UIViewController子类中声明一个HidingNavigationBarManager类型的成员变量。

Third, three lifecycle methods are used to make hidingNavBarManager work:

第三,使用三种生命周期方法来使hidingNavBarManager工作:

Two additional methods that are used in particular cases:

在特定情况下使用的两种其他方法:

Image for post

That is everything you need to hide/unhide your navigation bar while scrolling!

这就是滚动时隐藏/取消隐藏导航栏所需的一切!

将扩展视图添加到UINavigationBar (Add an extension view to the UINavigationBar)

Image for post

将ToolBar / TabBar添加到UINavigationBar (Add ToolBar/TabBar to the UINavigationBar)

Image for post

帮手 (Helpers)

应用程序前台时隐藏/显示/不执行任何操作 (Hide/show/do nothing when app is foregrounded)

耐膨胀性 (Expansion resistance)

As noted in the docs:

如文档中所述 :

“When the navigation bar is hidden, you can [add] some ‘resistance,’ which adds a delay before the navigation bar starts to expand when scrolling. The resistance value is the distance that the user needs to scroll before the navigation bar starts to expand.”

“隐藏导航栏时,您可以[添加]一些'阻力',这会增加在滚动时导航栏开始展开之前的延迟。 阻力值是用户在导航栏开始展开之前需要滚动的距离。”

hidingNavBarManager?.expansionResistance = 250

UIRefreshControl (UIRefreshControl)

If you are using a UIRefreshControl with your scroll view, it is important to let the HidingNavigationBarManagerknow about it:

如果在滚动视图中使用UIRefreshControl ,那么让HidingNavigationBarManager知道这一点很重要:

hidingNavBarManager?.refreshControl = refreshControl

您可能面对的问题 (Issues You May Face)

It seems as though HidingNavigationBarhas not been updated since Swift 3. As such, there are some minor language change problems. Luckily, Xcode is smart and all problems can be resolved by just clicking on the red error message on the right side of the editor. Well, all problems can be resolved except one:

自Swift 3以来,似乎HidingNavigationBar尚未更新。因此,存在一些较小的语言更改问题。 幸运的是,Xcode很聪明,只需单击编辑器右侧的红色错误消息即可解决所有问题。 好吧,除了以下一项,所有问题都可以解决:

This is the piece of code where the problem occurs. All you have to do is to change NSNotification.Name.UIApplication.didBecomeActiveNotification to UIApplication.didBecomeActiveNotification.

这是发生问题的代码。 您要做的就是将NSNotification.Name.UIApplication.didBecomeActiveNotification更改为UIApplication.didBecomeActiveNotification 。

什么时候应该使用HidingNavigationBar? (When Should You Use HidingNavigationBar?)

HidingNavigationBar is very easy to use and there is little to learn. However, I found out that if you use UIScrollView, there might be some problems with constraints and the whole framework as well. It stops showing/hiding the navigation items after a while. UITableView works perfectly, though.

HidingNavigationBar非常易于使用,几乎没有学习。 但是,我发现,如果使用UIScrollView ,则约束和整个框架都可能存在一些问题。 稍后,它将停止显示/隐藏导航项目。 UITableView可以完美地工作。

So if you need a fast and easy framework that will be linked to UITableView, this is the right choice.

因此,如果您需要一个快速简单的框架来链接到UITableView ,那么这是正确的选择。

结语 (Wrapping Up)

This is all you should know about HidingNavigationBar. I hope you found it interesting!

这就是您应该了解的HidingNavigationBar的全部。 希望您觉得有趣!

Take a look at the related project I created:

看一下我创建的相关项目:

If you want to learn more, check out the framework’s GitHub repo.

如果您想了解更多信息,请查看框架的GitHub repo 。

If you have any criticisms, questions, or suggestions, feel free to post them in the comments section below!

如果您有任何批评,问题或建议,请随时在下面的评论部分中发表!

Thanks for reading.

谢谢阅读。

翻译自: https://medium.com/better-programming/hide-and-show-the-navigation-bar-and-tab-bar-in-swift-a771550ed364

swift 中隐藏导航栏

更多推荐