-
Notifications
You must be signed in to change notification settings - Fork 234
Increasing safety in loadFromNib #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
f6a914e
beda40f
76b7478
9a0c647
cac93c8
5801ad9
659b3fa
688376e
3fc0c11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,8 @@ final class TableViewController: UITableViewController { | |
| } | ||
|
|
||
| override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { | ||
| let view = MyHeaderTableView.loadFromNib() | ||
| let view = MyHeaderTableView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: self.tableView(tableView, heightForHeaderInSection: section))) | ||
| view.loadNibContent() | ||
|
||
| view.fillForSection(section) | ||
| return view | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -297,17 +297,14 @@ final class MyCustomWidget: UIView, NibOwnerLoadable { | |
| … | ||
| required init?(coder aDecoder: NSCoder) { | ||
| super.init(coder: aDecoder) | ||
| MyCustomWidget.loadFromNib(owner: self) | ||
| } | ||
| override init(frame: CGRect) { | ||
| super.init(frame: frame) | ||
| self.loadNibContent() | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Overriding `init?(coder:)` allows your `MyCustomWidget` custom view to load its content from the associated XIB `MyCustomWidget.xib` and add it as subviews of itself. | ||
|
|
||
| _💡 Note: overriding `init(frame:)`, even just to call `super.init(frame: frame)` might seems pointless, but seems necessary in some cases due to a strange issue with Swift and dynamic dispatch not being able to detect and call the superclass implementation all by itself: I've sometimes seen crashes when not implementing it explicitly, so better safe than sorry._ | ||
| _💡 Note: possible to override `init(frame:)`, in order to be able to create an instance of that view programatically and call `loadNibContent()` to fill with views if needed. | ||
|
||
|
|
||
| ## 3b. Instantiating a `NibLoadable` view | ||
|
|
||
|
|
@@ -322,8 +319,6 @@ let view3 = NibBasedRootView.loadFromNib() // and another one | |
| … | ||
| ``` | ||
|
|
||
| > 💡 You could also use `MyCustomWidget.loadFromNib()` on a `NibOwnerLoadable` — the same way we just did on `NibLoadable` views above — to load them by code if needs be too. | ||
|
|
||
| --- | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,38 +41,22 @@ public extension NibOwnerLoadable where Self: UIView { | |
| - returns: A `NibOwnerLoadable`, `UIView` instance | ||
| */ | ||
| @discardableResult | ||
|
||
| static func loadFromNib(owner: Self) -> Self { | ||
| func loadNibContent() { | ||
| let layoutAttributes: [NSLayoutAttribute] = [.top, .leading, .bottom, .trailing] | ||
| for view in nib.instantiate(withOwner: owner, options: nil) { | ||
| for view in Self.nib.instantiate(withOwner: self, options: nil) { | ||
| if let view = view as? UIView { | ||
| view.translatesAutoresizingMaskIntoConstraints = false | ||
| owner.addSubview(view) | ||
| self.addSubview(view) | ||
| layoutAttributes.forEach { attribute in | ||
| owner.addConstraint(NSLayoutConstraint(item: view, | ||
| self.addConstraint(NSLayoutConstraint(item: view, | ||
| attribute: attribute, | ||
| relatedBy: .equal, | ||
| toItem: owner, | ||
| toItem: self, | ||
| attribute: attribute, | ||
| multiplier: 1, | ||
| constant: 0.0)) | ||
| } | ||
| } | ||
| } | ||
| return owner | ||
| } | ||
| } | ||
|
|
||
| public protocol FrameInitializable: class { | ||
| init(frame: CGRect) | ||
| } | ||
|
|
||
| public extension NibOwnerLoadable where Self: UIView, Self: FrameInitializable { | ||
| /** | ||
| Returns a `UIView` object instantiated from nib using a default owner instance | ||
|
|
||
| - returns: A `NibOwnerLoadable`, `UIView`, `FrameInitializable` instance | ||
| */ | ||
| static func loadFromNib() -> Self { | ||
| return Self.loadFromNib(owner: Self(frame: CGRect.zero)) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rework your entry so that it follows the same format as the other entries.
In particular, use a period + 2 spaces at the end of the lines describing the changes so that its properly formatted when the markdown is rendered (as a "new line in the same paragraph")
May I suggest something along those lines instead?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, although I'm gonna use "Enhancements"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops 😄 👍