iOS development—UI configuration file

When I first started learning iOS development, I ignored the power of having a configuration file for constants. To get the screen height, I would have to type UIScreen.main.bounds.height. It’s unnecessarily long. What if there is a shortcut? Consider the following code:

struct Screen {        
static let width: CGFloat = UIScreen.main.bounds.width
static let height: CGFloat = UIScreen.main.bounds.height

I can access the screen hight with Screen.Height. Better?

What if there is a background color that I use all over my app? Check this out:

struct UI {    
static let backgroundColor =

And a custom margin?

struct Margin {        
static let standard: CGFloat = 8
static let double: CGFloat = Margin.standard * 2
static let half: CGFloat = Margin.standard / 2

How about combine all of these together? Here is the gist:

I encourage you to create your own configuration file for these benefits:

  1. Each constant has a better meaning. Compare let height: CGFloat = 20 vs UI.StatusBar.height.
  2. If a constant changes in the future, all I have to do is to update this configuration file.
  3. Less typing. Reduce possibility for making a mistake.

Happy Coding!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.