Screen-based values in your iOS app
Configure interfaces depending on screen size of iPhone
Nowadays iOS developers must support around 6 different screens sizes of iPhones. This often requires to use different values when building interfaces for smaller and bigger devices. Usually, if you’re building interface from code, it looks like this somewhere in your view class:
This bunch of if/else doesn’t look sexy, and even worse, it has a chance to expand if you need to cover other devices separately. And when you have custom UI in your app, often you would need to provide such an approach in almost all of your screens.
So I wanted to improve this situation and thus I created SBValue.
SBValue
This is a simple structure, that accepts values of generic type, which associate with each of the iPhone devices. After the initialization of this struct, you just need to call definedValue
, and it will return a value, which corresponds to the device, that uses your app. That’s it.
You can add/customise init
methods in order to fit your requirments. And you can expand it with iPads as well.
To check for the device, that uses the app, I’ve implemented a popular solution:
Returning to previous example with ProfileView
, it will look like this after injection of SBValue there:
In result:
- less lines of code
- expandable for other devices
- easy to assign as a variable, when needed
You might be interested to check also an approach of my friend when you implement UI via interface builder.
If you have any questions or suggestions, feel free to contact me by email or just leave a comment below.
Hope you found this article useful and inspiring. Thanks for reading 🙌