我试图从窗口中删除标题和顶部按钮,基本上只显示内容。我尝试过各种方法但没有成功,也没有任何明显的原因说明它不起作用。 请参阅 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 View 。没什么特别的。
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.
}
}
}
最佳答案
您想要的是一个带有 NSWindowStyleMaskBorderless
类型掩码的窗口。问题是必须在初始化窗口时设置它。这就是为什么您的行 self.window?.styleMask = .borderless
无效。
因为您正在使用 Interface Builder 创建窗口,所以您必须打开相应的 xib
文件(我猜 MainMenu.xib
),选择该窗口并取消选择 属性检查器中的标题栏
:
Note that you can set a window’s or panel’s style mask to NSWindowStyleMaskBorderless in Interface Builder by deselecting Title Bar in the Appearance section of the Attributes inspector.
如果您想了解有关 NSWindow
结帐的不同样式的更多信息 NSWindowStyles通过 Luka Kerr 。这是一个很好的引用。
更新:在上述引用文献中,我找到了另一种删除标题栏的方法,可能适合您:
窗口?.styleMask.remove(.titled)
关于swift - 如何使用 Swift 和 MacOS 隐藏顶部栏(带按钮)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49663728/