Guest User

Untitled

a guest
Dec 11th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import UIKit
  2.  
  3. public protocol NibLoadable {
  4. static var nibName: String { get }
  5. }
  6.  
  7. public extension NibLoadable where Self: UIView {
  8.  
  9. public static var nibName: String {
  10. return String(describing: Self.self) // defaults to the name of the class implementing this protocol.
  11. }
  12.  
  13. public static var nib: UINib {
  14. let bundle = Bundle(for: Self.self)
  15. return UINib(nibName: Self.nibName, bundle: bundle)
  16. }
  17.  
  18. func setupFromNib() {
  19. guard let view = Self.nib.instantiate(withOwner: self, options: nil).first as? UIView else { fatalError("Error loading \(self) from nib") }
  20. addSubview(view)
  21. view.translatesAutoresizingMaskIntoConstraints = false
  22. if #available(iOS 11.0, *) {
  23. view.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
  24.  
  25. view.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
  26. view.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
  27. view.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
  28. } else {
  29. // Fallback on earlier versions
  30. }
  31.  
  32. }
  33. }
Add Comment
Please, Sign In to add comment