62 lines
1.9 KiB
Swift
62 lines
1.9 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 {
|
|
let parentWindow: UIWindow?
|
|
if #available(iOS 16.0, *),
|
|
let topWindow = UIApplication.shared.windows.last {
|
|
parentWindow = topWindow
|
|
} else {
|
|
parentWindow = UIApplication.shared.windows.first
|
|
}
|
|
var parent: UIViewController! = parentWindow?.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) }
|
|
}
|
|
}
|