@@ -12,9 +12,25 @@ public enum LayoutError: Error {
12
12
case fileNotFound( String )
13
13
}
14
14
15
- public protocol LayoutDelegate {
16
- func didCreate( _ view: UIView , with id: String )
17
- func formatOptions( for constraint: String ) -> NSLayoutConstraint . FormatOptions
15
+ @objc public protocol LayoutDelegate {
16
+ @objc optional func view( of type: String , for id: String ) -> UIView ?
17
+ @objc optional func didCreate( _ view: UIView , for id: String )
18
+ @objc optional func formatOptions( for constraint: String ) -> NSLayoutConstraint . FormatOptions
19
+ }
20
+
21
+ extension LayoutDelegate {
22
+
23
+ func view( of type: String , for id: String ) -> UIView ? {
24
+ return Layout . view ( of: type)
25
+ }
26
+
27
+ func didCreate( _ view: UIView , with id: String ) {
28
+ return
29
+ }
30
+
31
+ func formatOptions( for constraint: String ) -> NSLayoutConstraint . FormatOptions {
32
+ return [ ]
33
+ }
18
34
}
19
35
20
36
public class Layout {
@@ -42,9 +58,8 @@ public class Layout {
42
58
43
59
private func add( views: [ String : MarkupElement ] , to root: UIView ) {
44
60
for (id, view) in views {
45
- guard let cls = NSClassFromString ( view. type) as? UIView . Type else { continue }
46
- let v = cls. init ( )
47
- self . delegate? . didCreate ( v, with: id)
61
+ guard let v = self . delegate? . view ( of: view. type, for: id) ?? Layout . view ( of: view. type) else { continue }
62
+ self . delegate? . didCreate ? ( v, for: id)
48
63
v. translatesAutoresizingMaskIntoConstraints = false
49
64
v. tag = id. hash
50
65
self . views [ id] = v
@@ -67,5 +82,12 @@ public class Layout {
67
82
root. layoutSubviews ( )
68
83
}
69
84
85
+ public static func view( of type: String ) -> UIView ? {
86
+ if let cls = NSClassFromString ( type) as? UIView . Type {
87
+ let v = cls. init ( )
88
+ return v
89
+ }
90
+ return nil
91
+ }
92
+
70
93
}
71
-
0 commit comments