【问题标题】:makeTouchBar() not being called Mac Catalyst touchbarmakeTouchBar() 不被称为 Mac Catalyst touchbar
【发布时间】:2020-02-25 11:46:00
【问题描述】:

希望你能帮忙,我对此有点生气。我有一个 Objective-c iOS 项目,我正在使用 Catalyst 移植到顶级 MacOS。在我必须添加 Touchbar 支持之前,一切都很顺利。

我有一个我的主 viewController 的 swift 扩展,我在其中实现了 NSTouchBarDelegate 所需的委托方法

问题是,makeTouchBar 从未被调用过,无论是在 Swift 扩展中:

@objc override open func makeTouchBar() -> NSTouchBar? {
     let touchBar = NSTouchBar()
      touchBar.delegate = self
      touchBar.defaultItemIdentifiers = [...identifiers...]
      return touchBar
    }

或者只是在原始的 Obj-C viewController 中:

- (NSTouchBar *)makeTouchBar {
    return ...;
}

我遵循了多个教程,基本上都说“覆盖 makeTouchBar,它会神奇地工作”。我已成功地将工具栏和菜单添加到应用程序中,所以我有点难过发生了什么以及为什么系统永远不会调用此方法。

任何帮助将不胜感激

干杯 埃米尔

【问题讨论】:

    标签: ios objective-c swift mac-catalyst


    【解决方案1】:

    我遇到了类似的问题,这是由我的 SceneDelegate 实现 NSTouchBarDelegate 引起的。相反,我应该让我的 AppDelegate 实现 NSTouchBarDelegate。

    这是一个将 Touchbar 添加到 SwiftUI Catalyst 13.0 应用程序的工作示例:

    //
    //  TouchbarDelegate.swift
    //  TenderHub
    //
    //  Created by Peter Chapman on 5/08/20.
    //  Copyright © 2020 Conglomo Limited. All rights reserved.
    //
    
    import UIKit
    
    #if targetEnvironment(macCatalyst)
    extension NSTouchBarItem.Identifier {
        static let play = NSTouchBarItem.Identifier("play")
        static let trash = NSTouchBarItem.Identifier("trash")
    }
    
    extension AppDelegate: NSTouchBarDelegate {
        
        override func makeTouchBar() -> NSTouchBar? {
            let touchBar = NSTouchBar()
            touchBar.delegate = self
            
            touchBar.defaultItemIdentifiers = [
                .flexibleSpace,
                .play,
                .trash,
                .flexibleSpace
            ]
            
            return touchBar
        }
        
        func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
            let touchBarItem: NSTouchBarItem?
            
            switch identifier {
            case .play:
                guard let image = UIImage(systemName: "play") else { return nil }
                touchBarItem = NSButtonTouchBarItem(identifier: identifier,
                                                    image: image,
                                                    target: UIApplication.shared.openSessions.first?.scene?.delegate,
                                                    action: #selector(SceneDelegate.play(_:)))
                
            case .trash:
                guard let image = UIImage(systemName: "trash") else { return nil }
                touchBarItem = NSButtonTouchBarItem(identifier: identifier,
                                                    image: image,
                                                    target: UIApplication.shared.openSessions.first?.scene?.delegate,
                                                    action: #selector(SceneDelegate.trash(_:)))
            default:
                touchBarItem = nil
            }
            
            return touchBarItem
        }
    }
    #endif
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-28
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      相关资源
      最近更新 更多