BaseXibView.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // BaseXibView.swift
  3. // KMComponentLibrary
  4. //
  5. // Created by wanjun on 2024/6/6.
  6. //
  7. import Cocoa
  8. import AppKit
  9. @objcMembers
  10. public class BaseXibView: NSView {
  11. @IBOutlet var contentView: NSView!
  12. // private var area : NSTrackingArea?
  13. // MARK: 初始化
  14. public required init?(coder decoder: NSCoder) {
  15. super.init(coder: decoder)
  16. self.initContentView()
  17. }
  18. override init(frame frameRect: NSRect) {
  19. super.init(frame: frameRect)
  20. self.initContentView()
  21. }
  22. private func initContentView() {
  23. let isExist = Bundle.main.path(forResource: String(describing:self.classForCoder.self), ofType: "nib")
  24. if isExist != nil {
  25. var topLevelArray: NSArray? = nil
  26. let resource = NSNib(nibNamed: String(describing:self.classForCoder.self), bundle: Bundle(for: self.classForCoder.self))
  27. if resource != nil {
  28. if (resource!.instantiate(withOwner: self, topLevelObjects: &topLevelArray)) {
  29. for view in topLevelArray! {
  30. if view is NSView {
  31. contentView = view as? NSView
  32. break
  33. }
  34. }
  35. }
  36. if contentView == nil {
  37. contentView = NSView()
  38. }
  39. addSubview(contentView)
  40. contentView.translatesAutoresizingMaskIntoConstraints = false
  41. NSLayoutConstraint.activate([
  42. contentView.topAnchor.constraint(equalTo: topAnchor),
  43. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  44. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  45. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  46. contentView.updateConstraintsForSubtreeIfNeeded()
  47. }
  48. }
  49. }
  50. // public override func updateTrackingAreas() {
  51. // super.updateTrackingAreas()
  52. //
  53. // if let _area = area, _area.rect.isEmpty == false {
  54. // if (_area.rect.equalTo(bounds)) {
  55. // return
  56. // }
  57. // }
  58. //
  59. // if (area != nil) {
  60. // removeTrackingArea(area!)
  61. // area = nil
  62. // }
  63. //
  64. // area = NSTrackingArea(rect: bounds, options: [.mouseEnteredAndExited, .mouseMoved, .activeAlways], owner: self)
  65. // self.addTrackingArea(area!)
  66. // }
  67. //
  68. // public override func mouseEntered(with event: NSEvent) {
  69. // super.mouseEntered(with: event)
  70. //
  71. // }
  72. //
  73. // public override func mouseMoved(with event: NSEvent) {
  74. // super.mouseMoved(with: event)
  75. //
  76. // }
  77. //
  78. // public override func mouseExited(with event: NSEvent) {
  79. // super.mouseExited(with: event)
  80. //
  81. // }
  82. }