Last active
January 16, 2019 09:41
-
-
Save fromkk/d8e6a0f6697c17a3a0078cb07aa0dc8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Instantitable | |
import UIKit | |
public protocol Instantitable: class {} | |
public protocol StoryboardInstantitable: Instantitable { | |
static var viewControllerIdentifier: String? { get } | |
} | |
public protocol UINibInstantitable: Instantitable {} | |
public extension UINibInstantitable { | |
public static var nibName: String { return String(describing: self) } | |
public static var nibBundle: Bundle { return Bundle(for: self) } | |
public static func nib(name: String = Self.nibName, bundle: Bundle = Self.nibBundle) -> UINib { | |
return UINib(nibName: name, bundle: bundle) | |
} | |
public static func instantiate(name: String = Self.nibName, bundle: Bundle = Self.nibBundle, owner: Any? = nil, options: [UINib.OptionsKey: Any]? = nil) -> Self { | |
let nib = self.nib(name: name, bundle: bundle) | |
return nib.instantiate(withOwner: owner, options: options).first as! Self | |
} | |
} | |
public extension StoryboardInstantitable { | |
public static var storyboardName: String { return String(describing: self) } | |
public static var storyboardBundle: Bundle { return Bundle(for: self) } | |
public static func storyboard(name: String = Self.storyboardName, bundle: Bundle = Self.storyboardBundle) -> UIStoryboard { | |
return UIStoryboard(name: name, bundle: bundle) | |
} | |
public static func instantiate(name: String = Self.storyboardName, bundle: Bundle = Self.storyboardBundle, identifier: String? = Self.viewControllerIdentifier) -> Self { | |
let storyboard = self.storyboard(name: name, bundle: bundle) | |
if let identifier = identifier { | |
return storyboard.instantiateViewController(withIdentifier: identifier) as! Self | |
} else { | |
return storyboard.instantiateInitialViewController() as! Self | |
} | |
} | |
} | |
// Localizables | |
protocol Localizable: RawRepresentable { | |
var rawValue: String { get } | |
} | |
extension Localizable { | |
func localize(table: String? = "Localizable", bundle: Bundle = Bundle.main) -> String { | |
if nil == table { | |
return NSLocalizedString(self.rawValue, comment: "") | |
} else { | |
return NSLocalizedString(self.rawValue, tableName: table, bundle: bundle, value: "", comment: "") | |
} | |
} | |
} | |
// Reusables | |
public protocol Reusable: class {} | |
public extension Reusable { | |
static var reuseIdentifier: String { return String(describing: self) } | |
} | |
public protocol ReusableCell: Reusable {} | |
public protocol UITableViewCellReusable: ReusableCell {} | |
public protocol UICollectionViewCellReusable: ReusableCell {} | |
public extension UITableViewCellReusable { | |
public static func dequeue(_ view: UITableView, for indexPath: IndexPath) -> Self { | |
return view.dequeueReusableCell(withIdentifier: Self.reuseIdentifier, for: indexPath) as! Self | |
} | |
} | |
public extension UICollectionViewCellReusable { | |
public static func dequeue(_ view: UICollectionView, for indexPath: IndexPath) -> Self { | |
return view.dequeueReusableCell(withReuseIdentifier: Self.reuseIdentifier, for: indexPath) as! Self | |
} | |
} | |
public protocol ReusableView: Reusable {} | |
public protocol UITableViewHeaderFooterViewReusable: ReusableView {} | |
public protocol UICollectionReusableViewReusable: ReusableView {} | |
public extension UITableViewHeaderFooterViewReusable { | |
public static func dequeue(_ view: UITableView) -> Self { | |
return view.dequeueReusableHeaderFooterView(withIdentifier: Self.reuseIdentifier) as! Self | |
} | |
} | |
public extension UICollectionReusableViewReusable { | |
public static func dequeue(_ view: UICollectionView, of kind: String, for indexPath: IndexPath) -> Self { | |
return view.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: Self.reuseIdentifier, for: indexPath) as! Self | |
} | |
} | |
extension UITableView { | |
private func height(for headerFooterView: UIView) -> CGFloat { | |
let width: CGFloat = self.bounds.size.width | |
headerFooterView.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: width, height: 0.0)) | |
headerFooterView.translatesAutoresizingMaskIntoConstraints = false | |
let widthLayout: NSLayoutConstraint = NSLayoutConstraint(item: headerFooterView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: width) | |
headerFooterView.addConstraint(widthLayout) | |
headerFooterView.layoutIfNeeded() | |
headerFooterView.setNeedsLayout() | |
let height: CGFloat = headerFooterView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height | |
headerFooterView.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: width, height: height)) | |
headerFooterView.translatesAutoresizingMaskIntoConstraints = true | |
headerFooterView.removeConstraint(widthLayout) | |
return height | |
} | |
func autolayoutTableHeaderView() { | |
guard let tableHeaderView: UIView = self.tableHeaderView else { return } | |
let width: CGFloat = self.frame.size.width | |
let height: CGFloat = self.height(for: tableHeaderView) | |
tableHeaderView.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: width, height: height)) | |
self.tableHeaderView = tableHeaderView | |
} | |
func autolayoutTableFooterView() { | |
guard let tableFooterView: UIView = self.tableFooterView else { return } | |
let width: CGFloat = self.frame.size.width | |
let height: CGFloat = self.height(for: tableFooterView) | |
tableFooterView.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: width, height: height)) | |
self.tableFooterView = tableFooterView | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment