如何使用Swift和MacOS隐藏顶部栏(带按钮)?

时间:2018-04-05 03:34:35

标签: swift macos macos-high-sierra

我试图从窗口中删除标题和顶部按钮,基本上只显示内容。我没有成功地尝试了各种各样的事情,没有任何明显的理由它没有成功。  有关我尝试过的选项

,请参阅setVisibility函数

AppDelagate.swift

 import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    func setVisibility(){
        self.window?.titlebarAppearsTransparent = true;
        self.window?.styleMask = .borderless
        self.window?.titleVisibility = .hidden
        self.window?.styleMask.insert(.fullSizeContentView)
        /*
         self.window?.standardWindowButton(.zoomButton)!.isHidden = true
         self.window?.standardWindowButton(.closeButton)!.isHidden = true
         */
        self.window?.level = .floating
        self.window?.center()
        self.window?.collectionBehavior = .canJoinAllSpaces
        //self.window?.collectionBehavior = .moveToActiveSpace
    }

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        setVisibility()
        NSApp.activate(ignoringOtherApps: true)

    }
    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }

    func applicationDidBecomeActive(_ aNotification: Notification) {
       setVisibility()
        NSApp.activate(ignoringOtherApps: true)

    }


    func applicationWillResignActive(_ aNotification: Notification) {
        setVisibility()
        NSApp.activate(ignoringOtherApps: true)

    }


    func applicationWillEnterForeground(_ aNotification: Notification) {
        setVisibility()
        NSApp.activate(ignoringOtherApps: true)

    }

    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
        NSApp.terminate(self)
        return true
    }
}

ViewController.swift 我只是嵌入了一个webkit视图。没什么好看的。

import Cocoa
import WebKit
import os

class ViewController: NSViewController, WKUIDelegate, WKNavigationDelegate {
    @IBOutlet weak var window: NSWindow!



    let webView = WKWebView()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.webView.uiDelegate = self
        self.webView.navigationDelegate = self
        webView.frame = CGRect(x: 0, y: 0, width: 1200, height: 600)
         view.addSubview(webView)
        let url = URL(string: "http://localhost:8080/?x=y")
        //let url = URL(string: "https://apple.com")
        let request = URLRequest(url: url!)
        webView.load(request)

    }


    public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void)
    {
            if navigationAction.request.url?.path == "/close" || navigationAction.request.url?.path == "/close/"{
                exit(0);
            }
        decisionHandler(.allow)
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

1 个答案:

答案 0 :(得分:0)

你想要的是一个带有NSWindowStyleMaskBorderless类型样式掩码的窗口。问题是必须在初始化窗口时设置。这就是你的专栏self.window?.styleMask = .borderless没有效果的原因。

因为您使用Interface Builder创建窗口,所以必须打开相应的xib文件(我猜MainMenu.xib),选择窗口并在属性检查器中取消选择Title Bar

Title Bar

我知道。这令人困惑。但至少Apple在documentation中提及它:

  

请注意,您可以通过在“属性”检查器的“外观”部分中取消选择“标题栏”,在Interface Builder中将窗口或面板的样式蒙版设置为NSWindowStyleMaskBorderless。

如果您想通过NSWindowStyles详细了解NSWindow结帐Luka Kerr的不同风格。这是一个很好的参考。

更新:在上述参考资料中,我找到了另一种方法来删除可能适合您的标题栏:

window?.styleMask.remove(.titled)