-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNibloadable.swift
More file actions
41 lines (33 loc) · 1.17 KB
/
Nibloadable.swift
File metadata and controls
41 lines (33 loc) · 1.17 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
39
40
41
import UIKit
public protocol Identifiable {
static var identifier: String { get }
}
public extension Identifiable {
static var identifier: String {
return String(describing: self)
}
}
public protocol NibLoadable: AnyObject, Identifiable {
static var nibName: String { get }
}
public extension NibLoadable where Self: UIView {
static var nibName: String {
return String(describing: self)
}
static func instantiate() -> Self {
let views = Bundle.main.loadNibNamed(nibName,
owner: nil,
options: nil) ?? []
return views.lazy.compactMap { $0 as? UIView }.first! as! Self
}
}
extension UICollectionView {
func register<T: NibLoadable>(_ type: T.Type) {
let nib = UINib(nibName: type.nibName, bundle: .main)
register(nib, forCellWithReuseIdentifier: type.identifier)
}
func register<T: NibLoadable>(_ type: T.Type, forSupplementaryViewOfKind kind: String) {
let nib = UINib(nibName: type.nibName, bundle: .main)
register(nib, forSupplementaryViewOfKind: kind, withReuseIdentifier: type.identifier)
}
}