56 lines
1.6 KiB
Swift
56 lines
1.6 KiB
Swift
//
|
|
// UIViewController.swift
|
|
// List
|
|
//
|
|
// Created by Igor Danich on 30.06.2020.
|
|
// Copyright © 2020 Igor Danich. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
extension UIViewController {
|
|
static func loadFromNib() -> Self {
|
|
func instantiateFromNib<T: UIViewController>() -> T {
|
|
return T.init(nibName: String(describing: T.self), bundle: nil)
|
|
}
|
|
return instantiateFromNib()
|
|
}
|
|
}
|
|
|
|
// MARK: - REFACTOR
|
|
|
|
extension UIViewController {
|
|
func child<T:UIViewController>() -> T? { children.compactMap({ $0 as? T }).first }
|
|
static func topController() -> UIViewController {
|
|
var parent: UIViewController! = UIApplication.shared.windows.first?.rootViewController
|
|
while parent.presentedViewController != nil
|
|
|| (parent.presentationController?.presentedViewController != nil && parent.presentationController?.presentedViewController != parent)
|
|
{
|
|
parent = parent.presentedViewController ?? parent.presentationController?.presentedViewController
|
|
}
|
|
return parent
|
|
}
|
|
}
|
|
|
|
extension UIView {
|
|
var viewController: UIViewController? { (next as? UIViewController) ?? (next as? UIView)?.viewController }
|
|
}
|
|
|
|
extension UIViewController {
|
|
private struct Keys {
|
|
static var bag = "bag"
|
|
}
|
|
var bag: Notification.Bag {
|
|
get {
|
|
if let bag: Notification.Bag = Common.Runtime.object(self, key: &Keys.bag) {
|
|
return bag
|
|
} else {
|
|
let bag = Notification.Bag()
|
|
self.bag = bag
|
|
return bag
|
|
}
|
|
}
|
|
set { Common.Runtime.set(self, value: newValue, key: &Keys.bag) }
|
|
}
|
|
}
|