KMBaseXibView.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // KMBaseXibView.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/2/15.
  6. // xib初始化
  7. import Cocoa
  8. class KMBaseXibView: NSView {
  9. @IBOutlet var contentView: NSView!
  10. // MARK: 初始化
  11. public required init?(coder decoder: NSCoder) {
  12. super.init(coder: decoder)
  13. self.initContentView()
  14. self.setup()
  15. self.updateUI()
  16. self.updateLanguage()
  17. self.reloadData()
  18. self.addNotification()
  19. }
  20. override init(frame frameRect: NSRect) {
  21. super.init(frame: frameRect)
  22. self.initContentView()
  23. self.setup()
  24. self.updateUI()
  25. self.updateLanguage()
  26. self.reloadData()
  27. self.addNotification()
  28. }
  29. private func initContentView() {
  30. let isExist = Bundle.main.path(forResource: String(describing:self.classForCoder.self), ofType: "nib")
  31. if isExist != nil {
  32. var topLevelArray: NSArray? = nil
  33. //绑定xib
  34. let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
  35. bundle: nil)
  36. if resource != nil {
  37. if (resource!.instantiate(withOwner: self, topLevelObjects: &topLevelArray)) {
  38. for view in topLevelArray! {
  39. if view is NSView {
  40. contentView = view as? NSView
  41. break
  42. }
  43. }
  44. }
  45. if contentView == nil {
  46. contentView = NSView()
  47. }
  48. addSubview(contentView)
  49. contentView.translatesAutoresizingMaskIntoConstraints = false
  50. NSLayoutConstraint.activate([
  51. contentView.topAnchor.constraint(equalTo: topAnchor),
  52. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  53. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  54. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  55. contentView.updateConstraintsForSubtreeIfNeeded()
  56. }
  57. }
  58. }
  59. func setup() {
  60. }
  61. //刷新界面UI 和 数据
  62. func reloadData() {
  63. }
  64. func updateLanguage() {
  65. }
  66. func updateUI() {
  67. }
  68. func resetData() {
  69. }
  70. func addNotification() {
  71. }
  72. func removeNotification() {
  73. }
  74. }