iOS dark theme
I like applications that have implemented at least two color themes: light and dark. Especially now when the best mobile phones have OLED screens which can show perfect black color.
I’m creating simple application for keeping track of cryptocurrency prices and I decided to support two themes. After implementing all stuff my application looks like on following images.
And two more (with charts)…
I will describe shortly how I implemented that feature.
Storing chosen theme
In our application we have to store information about style chosen by the user. Of course the best solution is to choose
CoreData. We have to add a new entity e.g.
Settings and add one attribute
Obviously if we plan to support more then two themes we have to choose other data type (for example you can choose
Integer type and create enum type whith all themes).
Also I’ve created one simple helper class for retrieve/save settings.
Now when user chooses style we have to store that information in our application. When application starts we have to read that information and prepare proper style.
In our application we need to have place where user can choose style. When user changes the style we must send a signal to all our running views. For that purposes we will use notifications. First we have to prepare two custom notifications.
Now we should create action in our application where user can switch between themes. In my application I have that place in Settings view:
I have one action connected to
Dark mode switch.
Now when user changes theme we can catch that notification everywhere in the application.
Of course, it would not be optimal if we caught the notification separately in each view. We can create base controllers classes and customize most of the things here. When we have soemthing specific in our view we can override one of method:
Base controller for table views.
Base controller for view.
Now when user changes theme our notification is catched in one of our base controller and application is changing the style of views.
You can check the whole solution in GitHub repository:
I hope this helps you!