Guest User

Untitled

a guest
Jan 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. //
  2. // XibView.swift
  3. // XibView
  4. //
  5. // Created by Victor Pavlychko on 1/13/18.
  6. //
  7.  
  8. import UIKit
  9.  
  10. @IBDesignable
  11. open class XibView: UIView {
  12.  
  13. @IBOutlet open var contentView: UIView? {
  14. didSet {
  15. guard contentView != oldValue, let contentView = contentView else {
  16. return
  17. }
  18. oldValue?.removeFromSuperview()
  19. contentView.translatesAutoresizingMaskIntoConstraints = true
  20. contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  21. contentView.frame = bounds
  22. addSubview(contentView)
  23. }
  24. }
  25.  
  26. public override init(frame: CGRect) {
  27. super.init(frame: frame)
  28. instantiateNib(findNib())
  29. }
  30.  
  31. public required init?(coder aDecoder: NSCoder) {
  32. super.init(coder: aDecoder)
  33. instantiateNib(findNib())
  34. }
  35.  
  36. private func findNib() -> (Bundle, String) {
  37. let superclasses = sequence(first: type(of: self) as AnyClass, next: { return $0.superclass() })
  38. for cls in superclasses {
  39. let bundle = Bundle(for: cls)
  40. let name = String(describing: cls).components(separatedBy: ".").last ?? ""
  41. if bundle.url(forResource: name, withExtension: "nib") != nil {
  42. return (bundle, name)
  43. }
  44. }
  45. fatalError("Couldn't find nib for \(type(of: self))")
  46. }
  47.  
  48. private func instantiateNib(_ nib: (bundle: Bundle, name: String)) {
  49. nib.bundle.loadNibNamed(nib.name, owner: self, options: nil)
  50. }
  51. }
Add Comment
Please, Sign In to add comment