Skip to content

Commit

Permalink
Issue #55: migrate to Swift 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Zmicier Zaleznicenka authored and Zmicier Zaleznicenka committed Sep 28, 2017
1 parent 8415f70 commit df3c7b1
Showing 1 changed file with 19 additions and 37 deletions.
56 changes: 19 additions & 37 deletions NibDesignable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@
import UIKit

public protocol NibDesignableProtocol: NSObjectProtocol {
/**
Identifies the view that will be the superview of the contents loaded from
the Nib. Referenced in setupNib().
- returns: Superview for Nib contents.
*/
var nibContainerView: UIView { get }
// MARK: - Nib loading

/**
Called to load the nib in setupNib().
Expand Down Expand Up @@ -65,21 +57,21 @@ extension NibDesignableProtocol {

/**
Called in init(frame:) and init(aDecoder:) to load the nib and add it as a subview.
- parameter container: a view that will become the superview of the contents loaded from the Nib
*/
fileprivate func setupNib() {
fileprivate func setupNib(in container: UIView) {
let view = self.loadNib()
self.nibContainerView.addSubview(view)
container.addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
let bindings = ["view": view]
self.nibContainerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", options:[], metrics:nil, views: bindings))
self.nibContainerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options:[], metrics:nil, views: bindings))
container.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", options:[], metrics:nil, views: bindings))
container.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options:[], metrics:nil, views: bindings))
}
}

extension UIView {
public var nibContainerView: UIView {
return self
}

/**
Called in the default implementation of loadNib(). Default is class name.
Expand All @@ -96,52 +88,45 @@ open class NibDesignable: UIView, NibDesignableProtocol {
// MARK: - Initializer
override public init(frame: CGRect) {
super.init(frame: frame)
self.setupNib()
self.setupNib(in: self)
}

// MARK: - NSCoding
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupNib()
self.setupNib(in: self)
}
}

@IBDesignable
open class NibDesignableTableViewCell: UITableViewCell, NibDesignableProtocol {
public override var nibContainerView: UIView {
return self.contentView
}

// MARK: - Initializer
override public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.setupNib()
self.setupNib(in: self.contentView)
}

// MARK: - NSCoding
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupNib()
self.setupNib(in: self.contentView)
}
}

@IBDesignable
open class NibDesignableTableViewHeaderFooterView: UITableViewHeaderFooterView, NibDesignableProtocol {

public override var nibContainerView: UIView {
return self.contentView
}

// MARK: - Initializer
override public init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
self.setupNib()
self.setupNib(in: self)
}

// MARK: - NSCoding
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupNib()
self.setupNib(in: self)
}
}

Expand All @@ -151,13 +136,13 @@ open class NibDesignableControl: UIControl, NibDesignableProtocol {
// MARK: - Initializer
override public init(frame: CGRect) {
super.init(frame: frame)
self.setupNib()
self.setupNib(in: self)
}

// MARK: - NSCoding
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupNib()
self.setupNib(in: self)
}
}

Expand All @@ -167,31 +152,28 @@ open class NibDesignableCollectionReusableView: UICollectionReusableView, NibDes
// MARK: - Initializer
override public init(frame: CGRect) {
super.init(frame: frame)
self.setupNib()
self.setupNib(in: self)
}

// MARK: - NSCoding
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupNib()
self.setupNib(in: self)
}
}

@IBDesignable
open class NibDesignableCollectionViewCell: UICollectionViewCell, NibDesignableProtocol {
public override var nibContainerView: UIView {
return self.contentView
}

// MARK: - Initializer
override public init(frame: CGRect) {
super.init(frame: frame)
self.setupNib()
self.setupNib(in: self.contentView)
}

// MARK: - NSCoding
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupNib()
self.setupNib(in: self.contentView)
}
}

0 comments on commit df3c7b1

Please sign in to comment.