Do NOT use Storyboards or Xibs
Convention
In most cases on both macOS and iOS, it is recommended to avoid storyboards (and XIBs) in favor of programmatically creating views and view controllers
Rationale
- Runtime performance analysis has shown us that disk operations degrade poorly for users with slower devices. Since creating UI programmatically allows us to avoid reading the storyboard/nib from disk at runtime, we should do so.
- Storyboard XML is becoming simpler over time, but still falls short of human readability. This makes code reviews difficult and merge conflicts likely. The generated XML is easily bloated and it’s difficult to be sure everything we’re adding is necessary and intentional.
- Keeping view hierarchy construction in storyboards can leave code simpler, but there are some best practices that can keep code simple while avoiding the pitfalls above:
- Not supported in VisionOS
Exceptions
- The main menu bar for a macOS app is required to be specified in a XIB.
How to convert xib/storyboards to programmatic initialization on iOS
- Remove any reference to loading UIStoryboard pointer storyboardWithName:bundle:
- Change the UIViewController caller
- If you were using storyboards, make sure segues related functions do NOT get called after you convert them to programmatic UI setup.
- Initialize the UIViewController’s view appropriately.
- Override loadView. If UIViewController needs to have a custom view, make sure you set the view in loadView. If there was anything declared in awakeFromNib make sure it has been moved over to loadView appropriately. For more info, please refer to Apple documentation.
- Modify any properties that were declared with IBOutlet now need to be programmatically allocated it memory.
- Add any UIView to its self view’s hierarchy using [addSubview(:)](https://developer.apple.com/documentation/uikit/uiview/1622616-addsubview?language=objc).
- Set up all the subviews’ constraints
- You might need to set subviews’ translatesAutoresizingMaskIntoConstraints to false because it is by default true and it will conflict with auto-translated constraints.
- If you use UIStackView with init(arrangedSubviews:), it may minimize the number of manual constraints needed. You can also compose UIStackView inside another UIStackView. Nesting multiple stackviews effectively can make your code cleaner.
- Remove “Launch Screens” in your iOS application’s info.plist.
- Remove any reference to storyboard or xib files.
- Use key UILaunchScreen instead.
- Delete your xibs and storyboard files in your resource bundle.