本文介绍了所有窗口(包括其他应用程序窗口)顶部的应用程序窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢将NSWindow放在一切之上,包括其他应用程序窗口.这怎么可能?

I like to have my NSWindow top of everything including other app windows. How this is possible?

这是我的自定义NSWindow的外观吗?

This is how my custom NSWindow looks like?

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

And this how I open the NSWindowController from a button action

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

PlayerView.swift

import Cocoa
import AVFoundation

class PlayerView: NSView {

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

此PlayerView立即在此功能中填充CorePlayer.VideoViewController.swift

This PlayerView immediately fill with CorePlayer in this func.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)
}

推荐答案

尝试使用此

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

这篇关于所有窗口(包括其他应用程序窗口)顶部的应用程序窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:17