Latest versions of Xcode doesn't support custom file/project templates any more. Rest in peace ⚰️.
Xcode - custom UIView templates, that creates for you a nib - loadable UIView subclass with it's .xib file respectively.
It comes with Swift & Objective-C templates
- Other methods are error prone and not consistent with all other components of the UIKit
- This approach will work just fine with both initializers:
init?(coder aDecoder: NSCoder)init(frame: CGRect)
It means view user can initialize your custom view from code or just drag & drop it into any UI file (.storyboard or .xib).
- Because of how it is easy to setup this custom views you can compose them in any possible hierarchies without penalties by only using Xcode UI builder
Created file (.swift) will have following structure:
class TestView: UIView { @IBOutlet weak private(set) var contentView: UIView! required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) commonInit() } override init(frame: CGRect) { super.init(frame: frame) commonInit() } func commonInit() { loadFromNib() contentView.frame = bounds addSubview(contentView) }}
And a UIView extension if you opted in for it.You should do so only first time and it’s a good idea to move this extension to a separate file
extension UIView { func loadFromNib() { let nibName = String(describing: type(of: self)) let bundle = Bundle(for: type(of: self)) let nib = UINib(nibName: nibName, bundle: bundle) nib.instantiate(withOwner: self) }}
LetFancyCustomView be the class name indicated at creation, then you will have following files created for you
FancyCustomView.swiftFancyCustomView.xib
So, for nibName is enough to have:
let nibName = String(describing: type(of: self))
In this case thenibName will haveFancyCustomView value
Here we load class Bundle object:
let bundle = Bundle(for: type(of: self))
Initiating UINib file:
let nib = UINib(nibName: nibName, bundle: bundle)
But it actually only caches the contents of the nib file in memory, not doing actual work here; From Apple docs:
A UINib object caches the contents of a nib file in memory, ready for unarchiving and instantiation.
So, next we calling:
nib.instantiate(withOwner: self)
Only at this stagecontentView
and any other outlets get their values from .xib
Because of how nib loading work in UIKit, nib -File’s owner should be an object that is already initialized and this pattern can be observed already in:
- UIViewControllerwith view
- UITableViewCell / UICollectionViewCellwith contentView
So, from outside we will initialize only a wrapper that will load from .xib it’s content in form ofcontentView and any subviews added to it;
By defaultcontentView has autoresizingMask with autoresizing width & height and backgroundColor set to clear:
<autoresizingMask key=“autoresizingMask” widthSizable=“YES” heightSizable=“YES”/><color key=“backgroundColor” red=“0.0” green=“0.0” blue=“0.0” alpha=“0.0” colorSpace=“custom” customColorSpace=“sRGB”/>
That’s why this part is missing fromcommonInit.
Only customization needed tocontentView observed in code is:
contentView.frame = bounds
After this point there is no need to worry about layout if constraints are set correctly inside yourFancyCustomView.xib (remember UITableViewCell analogy, things here work in same way)
- Clone or download repository
- CopyCustom View.xctemplate folder to:
~/Library/Developer/Xcode/Templates/File Templates/User Interface/
- Access from Xcode menuNew File option.
- IniOS tab,User interface category findCustom view option.


- After you complete wizard steps, you will have in project your custom view files ( .swift + .xib or .h, .m + .xib).

- Go to a storyboard or any other .xib file and drag & drop fromObject Library a UIView object.

- For this object fromIdentity inspector set it'sCustom Class to your recently created UIView subclass.

- Ready to go :]