KMBaseXibView.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. }
  19. override init(frame frameRect: NSRect) {
  20. super.init(frame: frameRect)
  21. self.initContentView()
  22. self.setup()
  23. self.updateUI()
  24. self.updateLanguage()
  25. self.reloadData()
  26. self.addNotification()
  27. }
  28. private func initContentView() {
  29. let isExist = Bundle.main.path(forResource: String(describing:self.classForCoder.self), ofType: "nib")
  30. if isExist != nil {
  31. var topLevelArray: NSArray? = nil
  32. //绑定xib
  33. let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
  34. bundle: nil)
  35. if resource != nil {
  36. if (resource!.instantiate(withOwner: self, topLevelObjects: &topLevelArray)) {
  37. for view in topLevelArray! {
  38. if view is NSView {
  39. contentView = view as? NSView
  40. break
  41. }
  42. }
  43. }
  44. if contentView == nil {
  45. contentView = NSView()
  46. }
  47. addSubview(contentView)
  48. contentView.translatesAutoresizingMaskIntoConstraints = false
  49. NSLayoutConstraint.activate([
  50. contentView.topAnchor.constraint(equalTo: topAnchor),
  51. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  52. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  53. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  54. contentView.updateConstraintsForSubtreeIfNeeded()
  55. }
  56. }
  57. }
  58. func setup() {
  59. }
  60. //刷新界面UI 和 数据
  61. func reloadData() {
  62. }
  63. func updateLanguage() {
  64. }
  65. func updateUI() {
  66. }
  67. func resetData() {
  68. }
  69. func addNotification() {
  70. }
  71. func removeNotification() {
  72. }
  73. }