// // KMBaseXibView.swift // PDF Reader Pro // // Created by lizhe on 2023/2/15. // xib初始化 import Cocoa class KMBaseXibView: NSView { @IBOutlet var contentView: NSView! deinit { self.removeNotification() } // MARK: 初始化 public required init?(coder decoder: NSCoder) { super.init(coder: decoder) self.initContentView() self.setup() self.updateUI() self.updateLanguage() self.reloadData() self.addNotification() self.changeEffectiveAppearance() } override init(frame frameRect: NSRect) { super.init(frame: frameRect) self.initContentView() self.setup() self.updateUI() self.updateLanguage() self.reloadData() self.addNotification() self.changeEffectiveAppearance() } private func initContentView() { let isExist = Bundle.main.path(forResource: String(describing:self.classForCoder.self), ofType: "nib") if isExist != nil { var topLevelArray: NSArray? = nil //绑定xib let resource = NSNib(nibNamed: String(describing: self.classForCoder.self), bundle: nil) if resource != nil { if (resource!.instantiate(withOwner: self, topLevelObjects: &topLevelArray)) { for view in topLevelArray! { if view is NSView { contentView = view as? NSView break } } } if contentView == nil { contentView = NSView() } addSubview(contentView) contentView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ contentView.topAnchor.constraint(equalTo: topAnchor), contentView.leftAnchor.constraint(equalTo: leftAnchor), contentView.rightAnchor.constraint(equalTo: rightAnchor), contentView.bottomAnchor.constraint(equalTo: bottomAnchor)]) contentView.updateConstraintsForSubtreeIfNeeded() } } } // private func initContentView() { // //绑定xib // let resource = NSNib(nibNamed: String(describing: self.classForCoder.self), // bundle: Bundle(for: self.classForCoder.self))! // resource.instantiate(withOwner: self, topLevelObjects: nil) // addSubview(contentView) // contentView.translatesAutoresizingMaskIntoConstraints = false // NSLayoutConstraint.activate([ // contentView.topAnchor.constraint(equalTo: topAnchor), // contentView.leftAnchor.constraint(equalTo: leftAnchor), // contentView.rightAnchor.constraint(equalTo: rightAnchor), // contentView.bottomAnchor.constraint(equalTo: bottomAnchor)]) // contentView.updateConstraintsForSubtreeIfNeeded() // } func setup() { } //刷新界面UI 和 数据 func reloadData() { } func updateLanguage() { } func updateUI() { } func resetData() { } func addNotification() { NotificationCenter.default.addObserver(self, selector: #selector(changeEffectiveAppearance), name: NSNotification.Name(rawValue: "kEffectiveAppearance"), object: nil) } func removeNotification() { NotificationCenter.default.removeObserver(self) DistributedNotificationCenter.default().removeObserver(self) } @objc func changeEffectiveAppearance() { let isDarkModel = KMAdvertisementConfig.isDarkModel() if isDarkModel { self.appearance = NSAppearance(named: .darkAqua) } else { self.appearance = NSAppearance(named: .aqua) } self.updateUI() } }