【问题标题】:App window on top of all windows including others app windows应用程序窗口位于所有窗口之上,包括其他应用程序窗口
【发布时间】:2018-07-17 12:37:46
【问题描述】:

我喜欢将我的 NSWindow 放在包括其他应用程序窗口在内的所有内容之上。这怎么可能?

这就是我的自定义 NSWindow 的样子?

override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask,
              backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {

    super.init(contentRect: contentRect, styleMask: NSWindow.StyleMask.borderless,
               backing: NSWindow.BackingStoreType.buffered, defer: false)

    // Manage window appearance and size
    // this window manages video view controller
    self.backgroundColor = NSColor.clear
    self.alphaValue = CGFloat(AppSingleton.shared.getVideoOpacity()) / 100
    self.isOpaque = false

    self.collectionBehavior = .transient
    }

这就是我如何通过按钮操作打开 NSWindowController

func openPlayerWindow(url: URL) {
    let storyboard:NSStoryboard? = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: Bundle.main)
    let playerWindowController = storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "SIPlayerWindowController")) as! PlayerWindowController

    if let playerWindow = playerWindowController.window {
        playerWindowController.showWindow(nil)
        playerWindow.makeKeyAndOrderFront(self)
        NSApp.activate(ignoringOtherApps: true)

        let videoPlayerController = playerWindow.contentViewController as! VideoPlayerViewController
        videoPlayerController.videoFile = url
    }
}

PlayerView.swift

import Cocoa
import AVFoundation

class PlayerView: NSView {

    override func hitTest(_ point: NSPoint) -> NSView? {
        return nil;
    }
}

这个 PlayerView 立即在这个 func 中填充 CorePlayer。 VideoViewController.swift

func playVideo(videoFile: URL) {
        corePlayer.view().translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(corePlayer.view())

        NSLayoutConstraint.init(item: corePlayer.view(), attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint.init(item: corePlayer.view(), attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint.init(item: corePlayer.view(), attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint.init(item: corePlayer.view(), attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true

        corePlayer.playURL(videoFile.absoluteURL)
}

【问题讨论】:

    标签: swift macos operating-system nswindow


    【解决方案1】:

    尝试使用这个

        self.windowController?.showWindow(nil)
        self.makeKeyAndOrderFront(self)
        NSApp.activate(ignoringOtherApps: true)
    

    【讨论】:

    • 谢谢它正在工作,请参阅更新的覆盖初始化函数和新的 openPlayerWindow。
    • 如何通过我的顶部窗口(具有透明度)将鼠标和按键事件传递到其他应用程序窗口下方?
    • 尝试覆盖窗口视图中的函数 hitTest 并返回 nil。
    • CorePlayer 阻止这个有什么原因吗?看我的更新。谢谢。
    • 我找到了解决方案 self.ignoresMouseEvents = true;
    【解决方案2】:

    我找到了解决方案 self.ignoresMouseEvents = true;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 2016-02-24
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多