Last active
September 6, 2019 08:42
-
-
Save thanhluand/49a1d7d3f263ef82d5fa801588757540 to your computer and use it in GitHub Desktop.
Handle call class in storyboard easy
This file contains hidden or 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
import UIKit | |
extension UIViewController | |
{ | |
class func instantiateFromStoryboard(_ name: String = "Main") -> Self | |
{ | |
return instantiateFromStoryboardHelper(name) | |
} | |
fileprivate class func instantiateFromStoryboardHelper<T>(_ name: String) -> T | |
{ | |
let storyboard = UIStoryboard(name: name, bundle: nil) | |
let controller = storyboard.instantiateViewController(withIdentifier: String(describing: self)) as! T | |
return controller | |
} | |
} | |
// UIView. | |
extension UIView { | |
static var nib: UINib { | |
return UINib(nibName: "\(self)", bundle: nil) | |
} | |
static func instantiateFromNib() -> Self? { | |
func instanceFromNib<T: UIView>() ->T? { | |
return nib.instantiate() as? T | |
} | |
return instanceFromNib() | |
} | |
@IBInspectable | |
var cornerRadius: CGFloat { | |
get { | |
return layer.cornerRadius | |
} | |
set { | |
layer.cornerRadius = newValue | |
} | |
} | |
@IBInspectable | |
var borderWidthV: CGFloat { | |
get { | |
return layer.borderWidth | |
} | |
set { | |
layer.borderWidth = newValue | |
} | |
} | |
@IBInspectable | |
var borderColorV: UIColor? { | |
get { | |
if let color = layer.borderColor { | |
return UIColor(cgColor: color) | |
} | |
return nil | |
} | |
set { | |
if let color = newValue { | |
layer.borderColor = color.cgColor | |
} else { | |
layer.borderColor = nil | |
} | |
} | |
} | |
@IBInspectable | |
var shadowRadius: CGFloat { | |
get { | |
return layer.shadowRadius | |
} | |
set { | |
layer.shadowRadius = newValue | |
} | |
} | |
@IBInspectable | |
var shadowOpacity: Float { | |
get { | |
return layer.shadowOpacity | |
} | |
set { | |
layer.shadowOpacity = newValue | |
} | |
} | |
@IBInspectable | |
var shadowOffset: CGSize { | |
get { | |
return layer.shadowOffset | |
} | |
set { | |
layer.shadowOffset = newValue | |
} | |
} | |
@IBInspectable | |
var shadowColor: UIColor? { | |
get { | |
if let color = layer.shadowColor { | |
return UIColor(cgColor: color) | |
} | |
return nil | |
} | |
set { | |
if let color = newValue { | |
layer.shadowColor = color.cgColor | |
} else { | |
layer.shadowColor = nil | |
} | |
} | |
} | |
} | |
protocol ReusableView: class { | |
static var defaultReuseIdentifier: String { get } | |
static var nibName: String { get } | |
} | |
extension ReusableView where Self: UIView { | |
static var defaultReuseIdentifier: String { | |
// Get the real name of the class for default value | |
return String(describing: self) | |
} | |
static var nibName: String { | |
return NSStringFromClass(self).components(separatedBy: ".").last ?? "" | |
} | |
} | |
// UINib | |
extension UINib { | |
func instantiate() -> Any? { | |
return self.instantiate(withOwner: nil, options: nil).first | |
} | |
} | |
// UITableView | |
extension UITableView { | |
// Register the Cell from XIB with identifer | |
func register<T: UITableViewCell>(nibName name: T.Type, atBundle bundleClass: AnyClass? = nil) where T: ReusableView { | |
let identifier = T.defaultReuseIdentifier | |
let nibName = T.nibName | |
var bundle: Bundle? = nil | |
if let bundleName = bundleClass { | |
bundle = Bundle(for: bundleName) | |
} | |
register(UINib(nibName: nibName, bundle: bundle), forCellReuseIdentifier: identifier) | |
} | |
// DequeueReusableCell with identifier | |
func dequeueReusableCell<T: UITableViewCell>(_ type: T.Type) -> T where T: ReusableView { | |
guard let cell = self.dequeueReusableCell(withIdentifier: T.defaultReuseIdentifier) as? T else { | |
fatalError("Could not dequeue cell with identifier: \(T.defaultReuseIdentifier)") | |
} | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment