KMBaseXibView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // private func initContentView() {
  65. // //绑定xib
  66. // let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
  67. // bundle: Bundle(for: self.classForCoder.self))!
  68. // resource.instantiate(withOwner: self, topLevelObjects: nil)
  69. // addSubview(contentView)
  70. // contentView.translatesAutoresizingMaskIntoConstraints = false
  71. // NSLayoutConstraint.activate([
  72. // contentView.topAnchor.constraint(equalTo: topAnchor),
  73. // contentView.leftAnchor.constraint(equalTo: leftAnchor),
  74. // contentView.rightAnchor.constraint(equalTo: rightAnchor),
  75. // contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  76. // contentView.updateConstraintsForSubtreeIfNeeded()
  77. // }
  78. func setup() {
  79. }
  80. //刷新界面UI 和 数据
  81. func reloadData() {
  82. }
  83. func updateLanguage() {
  84. }
  85. func updateUI() {
  86. }
  87. func resetData() {
  88. }
  89. func addNotification() {
  90. NotificationCenter.default.addObserver(self, selector: #selector(changeEffectiveAppearance), name: NSNotification.Name(rawValue: "kEffectiveAppearance"), object: nil)
  91. }
  92. func removeNotification() {
  93. NotificationCenter.default.removeObserver(self)
  94. DistributedNotificationCenter.default().removeObserver(self)
  95. }
  96. @objc func changeEffectiveAppearance() {
  97. let isDarkModel = KMAdvertisementConfig.isDarkModel()
  98. if isDarkModel {
  99. self.appearance = NSAppearance(named: .darkAqua)
  100. } else {
  101. self.appearance = NSAppearance(named: .aqua)
  102. }
  103. self.updateUI()
  104. }
  105. }