Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. //MARK:- Get a class name
  2. extension NSObject {
  3. class var className: String {
  4. get {
  5. return NSStringFromClass(self).componentsSeparatedByString(".").last!
  6. }
  7. }
  8. //=> MyClass.className // "MyClass"
  9.  
  10. var className: String {
  11. get {
  12. return self.dynamicType.className
  13. }
  14. }
  15. //=> MyClass().className // "MyClass"
  16. }
  17.  
  18. //MARK:- UITableView
  19.  
  20. extension UITableView {
  21. func registerCell<T: UITableViewCell>(type: T.Type) {
  22. let className = type.className
  23. let nib = UINib(nibName: className, bundle: nil)
  24. registerNib(nib, forCellReuseIdentifier: className)
  25. }
  26. //=> tableView.registerCell(MyCell.self)
  27.  
  28. func dequeueCell<T: UITableViewCell>(type: T.Type, indexPath: NSIndexPath) -> T {
  29. return self.dequeueReusableCellWithIdentifier(type.className, forIndexPath: indexPath) as! T
  30. }
  31. //=> let cell = tableView.dequeueCell(MyCell.self)
  32. }
  33.  
  34. //MARK:- UICollectionView
  35.  
  36. extension UICollectionView {
  37. func registerCell<T: UICollectionViewCell>(type: T.Type) {
  38. let className = type.className
  39. let nib = UINib(nibName: type.className, bundle: nil)
  40. registerNib(nib, forCellWithReuseIdentifier: className)
  41. }
  42. //=> collectionView.registerCell(MyCell.self)
  43.  
  44. func registerReusableView<T: UICollectionReusableView>(type: T.Type, kind: String = UICollectionElementKindSectionHeader) {
  45. let className = type.className
  46. let nib = UINib(nibName: className, bundle: nil)
  47. registerNib(nib, forSupplementaryViewOfKind: kind, withReuseIdentifier: className)
  48. }
  49. //=> collectionView.registerReusableView(MyReusableView.self)
  50.  
  51. func dequeueCell<T: UICollectionViewCell>(type: T.Type, forIndexPath indexPath: NSIndexPath) -> T {
  52. return dequeueReusableCellWithReuseIdentifier(type.className, forIndexPath: indexPath) as! T
  53. }
  54. //=> let cell = collectionView.dequeueCell(MyCell.self)
  55.  
  56. func dequeueReusableView<T: UICollectionReusableView>(type: T.Type, indexPath: NSIndexPath, kind: String = UICollectionElementKindSectionHeader) -> T {
  57. return dequeueReusableSupplementaryViewOfKind(kind,
  58. withReuseIdentifier: type.className,
  59. forIndexPath: indexPath) as! T
  60. }
  61. //=> let view = collectionView.dequeueReusableView(type: MyReusableView.self, indexPath: indexPath)
  62. }
  63.  
  64. //MARK:- UIColor
  65.  
  66. extension UIColor {
  67. class func color(hex: Int, alpha: Double = 1.0) -> UIColor {
  68. let red = Double((hex & 0xFF0000) >> 16) / 255.0
  69. let green = Double((hex & 0xFF00) >> 8) / 255.0
  70. let blue = Double((hex & 0xFF)) / 255.0
  71.  
  72. return UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(alpha))
  73. }
  74. //=> let color = UIColor.color(0xAABBCC)
  75. }
  76.  
  77. //MARK:- UIApplication
  78.  
  79. extension UIApplication {
  80. func topViewController() -> UIViewController? {
  81. if var topViewController = UIApplication.sharedApplication().keyWindow?.rootViewController {
  82. while topViewController.presentedViewController != nil {
  83. topViewController = topViewController.presentedViewController!
  84. }
  85. return topViewController
  86. } else {
  87. return nil
  88. }
  89. }
  90. }
  91.  
  92. //MARK:- UIStoryboard
  93.  
  94. extension UIViewController {
  95. class func instantiate() -> Self {
  96. let storyboard = UIStoryboard(name: Self.className, bundle: nil)
  97. return storyboard.instantiateInitialViewController() as! Self
  98. }
  99. //=> MyViewController.instantiate()
  100.  
  101. class func instantiate(storyboard: String) -> Self {
  102. let storyboard = UIStoryboard(name: storyboard, bundle: nil)
  103. return storyboard.instantiateViewControllerWithIdentifier(Self.className) as! Self
  104. }
  105. //=> MyViewController.instantiate("MyStoryboard")
  106. }
  107.  
  108. //MARK:- UINib
  109.  
  110. extension UIView {
  111. class func instantiate<T: AnyObject>(owner: T) -> Self {
  112. return UINib(nibName: Self.className, bundle: nil).instantiateWithOwner(owner, options: nil)[0] as! Self
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement