UserInterfaceLevel, the future of floating apps?
iOS 13 brings new dynamism to your apps.
Last year at Shopify we started developing the All-new Point of Sale. There was a small problem, given that all screens are presented full screen to the user we needed the ability to distinguish a modal screen versus the rest. iOS 13 had a great answer for this.
It feels like every new iOS version adds something to
UIUserActivity. iOS 13 was no different, there was a small but powerful addition to
UITraitCollection which comes handy when developing your universal apps. Specially if your app is presented as a floating window, that addition was
What is UserInterfaceLevel?
Apple describes it as the visual level for content in a window. The possible values are
unspecified. The first one being the window’s main content and the last one anything above it*. This will be the perfect match for our need of modal windows having a dedicated background colour.
* Check the comment section for a great explaining from Geoff Hackworth about how these values changes depending on other factors
Using it to meet our requirement
The previous snippet is how Shopify’s Point of Sale application is handling dynamic colours. You should focus on line #24, we detect the current elevation of the user interface and decide which of the two colour sets to use for this particular colour.
Now, let’s say we want to change the background colour of a view all we need to do is:
And thanks to the excellent UIAppearance² we can style our UINavigationController similarly how we styled the background view, and all our screens will be displayed with the correct colour.
Putting everything together into a project, it looks something like this:
That is all!, as always you can find the full source code on GitHub
- Apple documentation https://developer.apple.com/documentation/uikit/uitraitcollection/3238085-userinterfacelevel
- Apple documentation https://developer.apple.com/documentation/uikit/uiappearance