Files
yingmeng-ios-switf/Pods/Reusable/Sources/View/NibLoadable.swift
2024-02-21 21:30:13 +08:00

50 lines
1.3 KiB
Swift
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*********************************************
*
* This code is under the MIT License (MIT)
*
* Copyright (c) 2016 AliSoftware
*
*********************************************/
#if canImport(UIKit)
import UIKit
// MARK: Protocol Definition
/// Make your UIView subclasses conform to this protocol when:
/// * they *are* NIB-based, and
/// * this class is used as the XIB's root view
///
/// to be able to instantiate them from the NIB in a type-safe manner
public protocol NibLoadable: AnyObject {
/// The nib file to use to load a new instance of the View designed in a XIB
static var nib: UINib { get }
}
// MARK: Default implementation
public extension NibLoadable {
/// By default, use the nib which have the same name as the name of the class,
/// and located in the bundle of that class
static var nib: UINib {
return UINib(nibName: String(describing: self), bundle: Bundle(for: self))
}
}
// MARK: Support for instantiation from NIB
public extension NibLoadable where Self: UIView {
/**
Returns a `UIView` object instantiated from nib
- returns: A `NibLoadable`, `UIView` instance
*/
static func loadFromNib() -> Self {
guard let view = nib.instantiate(withOwner: nil, options: nil).first as? Self else {
fatalError("The nib \(nib) expected its root view to be of type \(self)")
}
return view
}
}
#endif