如何使用 Swift 和 MacOS 隐藏顶部栏(带按钮)?
How to hide the top bar (with buttons) usin Swift and MacOS?
我正在尝试从 window 中删除标题和顶部按钮,基本上只显示内容。我已经尝试过各种方法但没有成功,但没有任何明显的原因表明它不起作用。
有关我尝试过的选项,请参阅 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.
}
}
}
您想要的是带有 NSWindowStyleMaskBorderless
类型样式掩码的 window。问题是这必须在初始化 window 时设置。这就是为什么你的行 self.window?.styleMask = .borderless
没有效果。
因为您正在使用 Interface Builder 创建 window,所以您必须打开相应的 xib
文件(我猜 MainMenu.xib
),select window 和 deselect Title Bar
在属性检查器中:
我知道。这令人困惑。但至少 Apple 在 documentation:
中提到了它
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 by Luka Kerr 的不同风格。这是一个很好的参考。
更新: 在上述参考资料中,我找到了另一种删除可能适合您的标题栏的方法:
window?.styleMask.remove(.titled)
我正在尝试从 window 中删除标题和顶部按钮,基本上只显示内容。我已经尝试过各种方法但没有成功,但没有任何明显的原因表明它不起作用。 有关我尝试过的选项,请参阅 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.
}
}
}
您想要的是带有 NSWindowStyleMaskBorderless
类型样式掩码的 window。问题是这必须在初始化 window 时设置。这就是为什么你的行 self.window?.styleMask = .borderless
没有效果。
因为您正在使用 Interface Builder 创建 window,所以您必须打开相应的 xib
文件(我猜 MainMenu.xib
),select window 和 deselect Title Bar
在属性检查器中:
我知道。这令人困惑。但至少 Apple 在 documentation:
中提到了它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 by Luka Kerr 的不同风格。这是一个很好的参考。
更新: 在上述参考资料中,我找到了另一种删除可能适合您的标题栏的方法:
window?.styleMask.remove(.titled)