UIView File Structuring

Samuel Hepditch
2 min readJan 1, 2022

iOS projects often have hundreds of classes inheriting from UIView. It is therefore highly important that developers decide on a consistent file structure for UIView classes across their project to increase readability and comprehension while decreasing development times and bugs. Below is the file structure I use for effective organization of classes inheriting from UIView.

Justification:

  • A rule of thumb in iOS development is to //MARK: — distinct sections. Adding this comment inserts a horizontal line above a section in your file and displays bolded text describing your section on the minimap. Mark sections for increased readability, efficient file navigation, and improved organization.
  • Always make use of extensions. Splitting up class functionality into extensions improves comprehension and readability.
  • Protocols associated with a view should appear at the beginning of the file. This is a design choice made for convenience. When implementing protocol stubs in a conforming view controller, developers commonly switch back and forth between multiple files to implement, edit and view protocol methods. If a protocol is positioned at the top of its related view file, it is more efficient to locate. The same reasoning explains why the first class extension in a view file should consist of the methods a view implements to conform with a protocol.
  • Note the button property in the gist above is a computed property. Using computed properties to generate subviews helps developers avoid massive initializers that are hard to read. Furthermore, in this format it is efficient to locate and edit view modifiers.
  • The order of the remaining extensions is unimportant. What matters most? developers choose a logical order and then remain consistent throughout their project.

For more posts related to mobile development and general software engineering concepts and ideas consider hitting that follow button. Until next time, happy coding!!!

--

--

Samuel Hepditch

Front End iOS Engineer at Pronti AI Inc. | Studying Computer Science at the University of Waterloo