123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- //
- // 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 = NSView.isDarkModel()
- if isDarkModel {
- self.appearance = NSAppearance(named: .darkAqua)
- } else {
- self.appearance = NSAppearance(named: .aqua)
- }
- self.updateUI()
- }
- }
|