KMBaseXibView.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // KMBaseXibView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/2/15.
  6. // xib初始化
  7. import Cocoa
  8. class KMBaseXibView: NSView {
  9. @IBOutlet var contentView: NSView!
  10. deinit {
  11. self.removeNotification()
  12. }
  13. // MARK: 初始化
  14. public required init?(coder decoder: NSCoder) {
  15. super.init(coder: decoder)
  16. self.initContentView()
  17. self.setup()
  18. self.updateUI()
  19. self.updateLanguage()
  20. self.reloadData()
  21. self.addNotification()
  22. self.changeEffectiveAppearance()
  23. }
  24. override init(frame frameRect: NSRect) {
  25. super.init(frame: frameRect)
  26. self.initContentView()
  27. self.setup()
  28. self.updateUI()
  29. self.updateLanguage()
  30. self.reloadData()
  31. self.addNotification()
  32. self.changeEffectiveAppearance()
  33. }
  34. private func initContentView() {
  35. let isExist = Bundle.main.path(forResource: String(describing:self.classForCoder.self), ofType: "nib")
  36. if isExist != nil {
  37. var topLevelArray: NSArray? = nil
  38. //绑定xib
  39. let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
  40. bundle: nil)
  41. if resource != nil {
  42. if (resource!.instantiate(withOwner: self, topLevelObjects: &topLevelArray)) {
  43. for view in topLevelArray! {
  44. if view is NSView {
  45. contentView = view as? NSView
  46. break
  47. }
  48. }
  49. }
  50. if contentView == nil {
  51. contentView = NSView()
  52. }
  53. addSubview(contentView)
  54. contentView.translatesAutoresizingMaskIntoConstraints = false
  55. NSLayoutConstraint.activate([
  56. contentView.topAnchor.constraint(equalTo: topAnchor),
  57. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  58. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  59. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  60. contentView.updateConstraintsForSubtreeIfNeeded()
  61. }
  62. }
  63. }
  64. func setup() {
  65. }
  66. //刷新界面UI 和 数据
  67. func reloadData() {
  68. }
  69. func updateLanguage() {
  70. }
  71. func updateUI() {
  72. }
  73. func resetData() {
  74. }
  75. func addNotification() {
  76. NotificationCenter.default.addObserver(self, selector: #selector(changeEffectiveAppearance), name: NSNotification.Name(rawValue: "kEffectiveAppearance"), object: nil)
  77. }
  78. func removeNotification() {
  79. NotificationCenter.default.removeObserver(self)
  80. DistributedNotificationCenter.default().removeObserver(self)
  81. }
  82. @objc func changeEffectiveAppearance() {
  83. let isDarkModel = KMAdvertisementConfig.isDarkModel()
  84. if isDarkModel {
  85. self.appearance = NSAppearance(named: .darkAqua)
  86. } else {
  87. self.appearance = NSAppearance(named: .aqua)
  88. }
  89. self.updateUI()
  90. }
  91. }