Recommendations for UIView structure in Swift
A breakdown our recommended file structure for Swift UIViews in iOS. Includes gists and a Github repository.
The following article breaks down our recommended file structure for Swift UIViews in iOS and follows the example that is available in our Open Source project.
Recommended Structure
We recommend that you create a Custom UIView that has the following files and functional extensions (i.e. +UI /+Update):
Declaration (CustomUIView.swift)
This is the base file where you declare your variables, UI and the basic lifecycle (initialize, custom inits, removeFromSuperview, viewWillTransition etc...).
Below is the code for a boilerplate CustomUIView.swift
Here is the code for one that calls all the setup functions for the extensions.
User Interface (+UI)
The +UI extension is where you manage the setup for your user interface.
It must start with func setupUI() and every subsequent functionality must be private.
Below is some sample code.
Notifications (+Notifications)
The +Notifications extension is where you manage the setup and functionality of all notification listeners.
It must start with func setupNotifications() and every subsequent functionality must be private.
Below is some sample code.
Gestures (+Gestures)
The +Gestures extension is where you manage the setup and functionality of all gestures.
It must start with func setupGestures() and every subsequent functionality must be private.
Below is some sample code.
Custom Touches (+Touches)
The +Touches extension is where you manage all custom touches for the view, such as touchesBegan, touchesMoved, touchesCancelled or touchesEnded.
Any animation functionality for these touches would be placed in the +Animations extension.
Below is some sample code.
Animations (+Animations)
The +Animations extension is where you manage all animations for the view, such as:
- Updates for touch states (func onTouchDownAnimation() or func onReleaseAnimation()).
- Updates for animating in and out of the screen (func onAnimateIn() or func onAnimateOut())
Below is some sample code.
Update (+Update)
The +Update extension is where you manage:
- Updates for your view for State updates (func onStateUpdate()).
- Updates for constraints (func updateLayoutConstraints()).
- Updates for language content updates (func onContentUpdate()).
- Other updates, such as an updated data set.
Below is some sample code.
Any Questions
We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.