-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathNibLoadable.swift
More file actions
38 lines (34 loc) · 1.05 KB
/
NibLoadable.swift
File metadata and controls
38 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// NibLoadable.swift
// Lockdown
//
// Created by Alexander Parshakov on 9/5/22
// Copyright © 2022 Confirmed Inc. All rights reserved.
//
import Foundation
import UIKit
public protocol NibLoadable: AnyObject {
/// The nib file to use to load a new instance of the View designed in a XIB
static var nib: UINib { get }
}
// MARK: Default implementation
public extension NibLoadable {
/// By default, use the nib which have the same name as the name of the class,
/// and located in the bundle of that class
static var nib: UINib {
return UINib(nibName: String(describing: self), bundle: Bundle(for: self))
}
}
// MARK: Support for instantiation from NIB
public extension NibLoadable where Self: UIView {
/**
Returns a `UIView` object instantiated from nib
- returns: A `NibLoadable`, `UIView` instance
*/
static func loadFromNib() -> Self {
guard let view = nib.instantiate(withOwner: nil, options: nil).first as? Self else {
fatalError("The nib \(nib) expected its root view to be of type \(self)")
}
return view
}
}