Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MARK:- Get a class name
- extension NSObject {
- class var className: String {
- get {
- return NSStringFromClass(self).componentsSeparatedByString(".").last!
- }
- }
- //=> MyClass.className // "MyClass"
- var className: String {
- get {
- return self.dynamicType.className
- }
- }
- //=> MyClass().className // "MyClass"
- }
- //MARK:- UITableView
- extension UITableView {
- func registerCell<T: UITableViewCell>(type: T.Type) {
- let className = type.className
- let nib = UINib(nibName: className, bundle: nil)
- registerNib(nib, forCellReuseIdentifier: className)
- }
- //=> tableView.registerCell(MyCell.self)
- func dequeueCell<T: UITableViewCell>(type: T.Type, indexPath: NSIndexPath) -> T {
- return self.dequeueReusableCellWithIdentifier(type.className, forIndexPath: indexPath) as! T
- }
- //=> let cell = tableView.dequeueCell(MyCell.self)
- }
- //MARK:- UICollectionView
- extension UICollectionView {
- func registerCell<T: UICollectionViewCell>(type: T.Type) {
- let className = type.className
- let nib = UINib(nibName: type.className, bundle: nil)
- registerNib(nib, forCellWithReuseIdentifier: className)
- }
- //=> collectionView.registerCell(MyCell.self)
- func registerReusableView<T: UICollectionReusableView>(type: T.Type, kind: String = UICollectionElementKindSectionHeader) {
- let className = type.className
- let nib = UINib(nibName: className, bundle: nil)
- registerNib(nib, forSupplementaryViewOfKind: kind, withReuseIdentifier: className)
- }
- //=> collectionView.registerReusableView(MyReusableView.self)
- func dequeueCell<T: UICollectionViewCell>(type: T.Type, forIndexPath indexPath: NSIndexPath) -> T {
- return dequeueReusableCellWithReuseIdentifier(type.className, forIndexPath: indexPath) as! T
- }
- //=> let cell = collectionView.dequeueCell(MyCell.self)
- func dequeueReusableView<T: UICollectionReusableView>(type: T.Type, indexPath: NSIndexPath, kind: String = UICollectionElementKindSectionHeader) -> T {
- return dequeueReusableSupplementaryViewOfKind(kind,
- withReuseIdentifier: type.className,
- forIndexPath: indexPath) as! T
- }
- //=> let view = collectionView.dequeueReusableView(type: MyReusableView.self, indexPath: indexPath)
- }
- //MARK:- UIColor
- extension UIColor {
- class func color(hex: Int, alpha: Double = 1.0) -> UIColor {
- let red = Double((hex & 0xFF0000) >> 16) / 255.0
- let green = Double((hex & 0xFF00) >> 8) / 255.0
- let blue = Double((hex & 0xFF)) / 255.0
- return UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(alpha))
- }
- //=> let color = UIColor.color(0xAABBCC)
- }
- //MARK:- UIApplication
- extension UIApplication {
- func topViewController() -> UIViewController? {
- if var topViewController = UIApplication.sharedApplication().keyWindow?.rootViewController {
- while topViewController.presentedViewController != nil {
- topViewController = topViewController.presentedViewController!
- }
- return topViewController
- } else {
- return nil
- }
- }
- }
- //MARK:- UIStoryboard
- extension UIViewController {
- class func instantiate() -> Self {
- let storyboard = UIStoryboard(name: Self.className, bundle: nil)
- return storyboard.instantiateInitialViewController() as! Self
- }
- //=> MyViewController.instantiate()
- class func instantiate(storyboard: String) -> Self {
- let storyboard = UIStoryboard(name: storyboard, bundle: nil)
- return storyboard.instantiateViewControllerWithIdentifier(Self.className) as! Self
- }
- //=> MyViewController.instantiate("MyStoryboard")
- }
- //MARK:- UINib
- extension UIView {
- class func instantiate<T: AnyObject>(owner: T) -> Self {
- return UINib(nibName: Self.className, bundle: nil).instantiateWithOwner(owner, options: nil)[0] as! Self
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement