What should you know about a navigation history stack in iOS 14

栏目: IT技术 · 发布时间: 6年前

内容简介:Prior to iOS 14, If you are deep down in a navigation stack, the only way to go back is to smash your back button until you reach the root level. This is no longer a case in iOS 14.With iOS 14, if you're deep down in a navigation stack and want to head bac

Prior to iOS 14, If you are deep down in a navigation stack, the only way to go back is to smash your back button until you reach the root level. This is no longer a case in iOS 14.

With iOS 14, if you're deep down in a navigation stack and want to head back to any level quickly, you can long-press the back button to bring up the history stack.

What should you know about a navigation history stack in iOS 14
Example of a history stack of Settings

Why should we care about this?

At first glance, this looks like a nice feature and nothing we need to do to adopt this. We get this behavior automatically when building our app against iOS 14 SDK.

The problem is, it is quite a trend nowadays that designers would design an app without a back button title. The following are some example apps without a back button title.

What should you know about a navigation history stack in iOS 14
Apps that have no back button title. Pinterest (left), Tasty (Middle), and Google news (Right)

Without a back button title, history stack will look like this (I assume you hide back button title withthis method).

What should you know about a navigation history stack in iOS 14
History stack without back button title

Where does history stack pick up the title?

To solve this problem, we need to know which string is used as titles in a history stack. From my testing, a history stack getting the title from a navigation back button title, not the navigation title.

Let's test this out in different scenarios. In the following examples, I set up an AViewController where the right navigation item is pushing another AViewController to itself.

If we set only title , this title will also use as a back button title and also use in a history stack.

class AViewController: UIViewController {
    override func viewDidLoad() {
        title = "Title A"
    }
}
What should you know about a navigation history stack in iOS 14
Back button title is getting from the title

If your title is long enough, the back button title will be shortened to just "Back" for saving space, but this won't affect a title in a history stack.

What should you know about a navigation history stack in iOS 14
Back button title show as "Back" for a long title
What should you know about a navigation history stack in iOS 14
Title show in history stack still use the full title

Customize back button title

As mentioned before, a back button title string will be used in a history stack. So, the text in backButtonTitle will be used as a title in a history stack if set.

class AViewController: UIViewController {
    override func viewDidLoad() {
        title = "Title A"
        navigationItem.backButtonTitle = "Custom back title A"
    }
}
What should you know about a navigation history stack in iOS 14
Custom back button title
What should you know about a navigation history stack in iOS 14
Value in backButtonTitle will be used as history title

Since history stack depends on the back button title, set empty title won't affect a history stack. You can design an app with or without a title since this won't affect a history stack.

class AViewController: UIViewController {
    override func viewDidLoad() {
        title = ""
        navigationItem.backButtonTitle = "Custom back title A"
    }
}
What should you know about a navigation history stack in iOS 14
Empty title

No back button title

This is the most interesting scenario of all since an empty back button title is quite popular among designers.

class AViewController: UIViewController {
    override func viewDidLoad() {
        title = "Title A"
        navigationItem.backButtonTitle = ""
    }
}
What should you know about a navigation history stack in iOS 14
Empty back title
What should you know about a navigation history stack in iOS 14
Empty back title will result in empty history title

As you can see, this is the most harmful design for iOS 14. Does this mean we have to tell our designers to put back a button title from now on? Luckily, the answer is no. We can work around this problem.

We know that a back button title must be there for history stack to pick up a title. So, we have to set backButtonTitle a value you want to show in history and then try to hide it in UI.

Change offset (not work)

The first idea that comes up is setting negative x offset like what we sometimes do with a CSS styling. You can hide a back button title with this method, but you will suffer from transition animation. You will see a title slide beyond the back button indicator.

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal:-500, vertical: 0), for: .compact)
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal:-500, vertical: 0), for: .default)
What should you know about a navigation history stack in iOS 14
Try to hide back button title by set negative offset

Transparent color

This solution is quite straight forward. We set a back button title color to transparent. This method uses some knowledge from UINavigationBar changes in iOS13 , you might want to check it out.

let appearance = UINavigationBarAppearance()
appearance.configureWithDefaultBackground()

appearance.backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.clear]

UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
What should you know about a navigation history stack in iOS 14
Transition animation look perfect with clear color
What should you know about a navigation history stack in iOS 14
History stack show as normal

This looks like a subtle change, but it requires some extra work on both the design and implementation phases. You can continue to design an app without a back button title, but make sure you provide a name for each view for a history stack.


以上所述就是小编给大家介绍的《What should you know about a navigation history stack in iOS 14》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

电子商务:管理与社交网络视角(原书第7版)

电子商务:管理与社交网络视角(原书第7版)

(美)埃弗雷姆·特班(Efraim Turban)、戴维.金(David King)、李在奎、梁定澎、德博拉·特班(Deborrah Turban) / 时启亮、陈育君、占丽 / 机械工业出版社 / 2014-1-1 / 79.00元

本书对电子学习、电子政务、基于web的供应链、协同商务等专题进行了详细的介绍,全书涵盖丰富的资料以及个案,讨论了Web 2.0环境内的产业结构、竞争变化以及对当今社会的影响。另外,本书在消费者行为、协同商务、网络安全、网络交易及客户管理管理、电子商务策略等内容上都有最新的改编,提供读者最新颖的内容,贴近当代电子商务的现实。 本书适合高等院校电子商务及相关专业的本科生、研究生及MBA学员,也可......一起来看看 《电子商务:管理与社交网络视角(原书第7版)》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具