Skip to content

Commit 0f87b0c

Browse files
committed
more delegate
1 parent bd67f12 commit 0f87b0c

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

Layout.swift

+29-7
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,25 @@ public enum LayoutError: Error {
1212
case fileNotFound(String)
1313
}
1414

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+
}
1834
}
1935

2036
public class Layout {
@@ -42,9 +58,8 @@ public class Layout {
4258

4359
private func add(views: [String: MarkupElement], to root: UIView) {
4460
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)
4863
v.translatesAutoresizingMaskIntoConstraints = false
4964
v.tag = id.hash
5065
self.views[id] = v
@@ -67,5 +82,12 @@ public class Layout {
6782
root.layoutSubviews()
6883
}
6984

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+
7093
}
71-

0 commit comments

Comments
 (0)