Swift ui mac os 菜单栏弹出框更改背景 [英] Swift ui mac os menubar popover change background
本文介绍了Swift ui mac os 菜单栏弹出框更改背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
正如您从图片中看到的,我有 menu bar popover
它是 transparent
,我希望它是一种特定的颜色.
我该怎么办,你能帮我一把吗?
状态栏控制器
import AppKit导入 SwiftUI类状态栏控制器{@ObservedObject var userPreferences = UserPreferences.instance私有 var statusBar: NSStatusBarvar statusItem: NSStatusItem私有变量 popover:NSPopoverinit(_ popover: NSPopover) {self.popover = 弹出框statusBar = NSStatusBar.init()statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)如果让 statusBarButton = statusItem.button {statusBarButton.image = #imageLiteral(资源名称:叉")statusBarButton.image?.size = NSSize(width: 18.0, height: 18.0)statusBarButton.image?.isTemplate = truestatusBarButton.action = #selector(togglePopover(sender:))statusBarButton.target = selfstatusBarButton.imagePosition = NSControl.ImagePosition.imageLeft}}@objc func togglePopover(发件人:AnyObject){如果(popover.isShown){hidePopover(发件人)}别的 {showPopover(发件人)}}func showPopover(_ 发件人: AnyObject) {如果让 statusBarButton = statusItem.button {popover.show(relativeTo: statusBarButton.bounds, of: statusBarButton, preferredEdge: NSRectEdge.maxY)}}func hidePopover(_ sender: AnyObject) {popover.performClose(发件人)}}
应用委托
导入可可导入 SwiftUI@主要的类 AppDelegate: NSObject, NSApplicationDelegate {var statusBar: StatusBarController?var popover = NSPopover.init()var 计时器:计时器?= 零func applicationDidFinishLaunching(_ aNotification: Notification) {让 contentView = ContentView()popover.contentSize = NSSize(宽度:360,高度:360)popover.contentViewController = NSHostingController(rootView: contentView)statusBar = StatusBarController.init(popover)}func applicationWillTerminate(_ aNotification: Notification) {//在此处插入代码以拆除您的应用程序}}
解决方案
参考:
使用这个扩展:
扩展 NSPopover {私有结构键{静态 var backgroundViewKey = "backgroundKey";}私有变量背景视图:NSView {让 bgView = objc_getAssociatedObject(self, &Keys.backgroundViewKey) as?视图如果让视图 = bgView {返回视图}让视图 = NSView()objc_setAssociatedObject(self, &Keys.backgroundViewKey, view, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)NotificationCenter.default.addObserver(self, selector: #selector(popoverWillOpen(_:)), name: NSPopover.willShowNotification, object: nil)返回视图}@objc 私有 func popoverWillOpen(_ 通知:通知) {如果 backgroundView.superview == nil {如果让 contentView = contentViewController?.view,让 frameView = contentView.superview {frameView.wantsLayer = 真backgroundView.frame = NSInsetRect(frameView.frame, 1, 1)backgroundView.autoresizingMask = [.width, .height]frameView.addSubview(backgroundView, 定位: .below, relativeTo: contentView)}}}var backgroundColor: NSColor?{得到 {如果让 bgColor = backgroundView.layer?.backgroundColor {返回 NSColor(cgColor: bgColor)}返回零}放 {backgroundView.wantsLayer = 真backgroundView.layer?.backgroundColor = newValue?.cgColor}}}
用法:
self.popover = popoverself.popover.backgroundColor = #colorLiteral(红色:0.3411764801,绿色:0.6235294342,蓝色:0.1686274558,alpha:1)
As you can see from the image I have the menu bar popover
which is transparent
, I would like it to be a specific color instead.
How can I do, can you give me a hand?
StatusBarController
import AppKit
import SwiftUI
class StatusBarController {
@ObservedObject var userPreferences = UserPreferences.instance
private var statusBar: NSStatusBar
var statusItem: NSStatusItem
private var popover: NSPopover
init(_ popover: NSPopover) {
self.popover = popover
statusBar = NSStatusBar.init()
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let statusBarButton = statusItem.button {
statusBarButton.image = #imageLiteral(resourceName: "Fork")
statusBarButton.image?.size = NSSize(width: 18.0, height: 18.0)
statusBarButton.image?.isTemplate = true
statusBarButton.action = #selector(togglePopover(sender:))
statusBarButton.target = self
statusBarButton.imagePosition = NSControl.ImagePosition.imageLeft
}
}
@objc func togglePopover(sender: AnyObject) {
if(popover.isShown) {
hidePopover(sender)
}else {
showPopover(sender)
}
}
func showPopover(_ sender: AnyObject) {
if let statusBarButton = statusItem.button {
popover.show(relativeTo: statusBarButton.bounds, of: statusBarButton, preferredEdge: NSRectEdge.maxY)
}
}
func hidePopover(_ sender: AnyObject) {
popover.performClose(sender)
}
}
AppDelegate
import Cocoa
import SwiftUI
@main
class AppDelegate: NSObject, NSApplicationDelegate {
var statusBar: StatusBarController?
var popover = NSPopover.init()
var timer: Timer? = nil
func applicationDidFinishLaunching(_ aNotification: Notification) {
let contentView = ContentView()
popover.contentSize = NSSize(width: 360, height: 360)
popover.contentViewController = NSHostingController(rootView: contentView)
statusBar = StatusBarController.init(popover)
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
解决方案
Reference: https://www.programmersought.com/article/29256315686/
Result:
Use this extension:
extension NSPopover {
private struct Keys {
static var backgroundViewKey = "backgroundKey"
}
private var backgroundView: NSView {
let bgView = objc_getAssociatedObject(self, &Keys.backgroundViewKey) as? NSView
if let view = bgView {
return view
}
let view = NSView()
objc_setAssociatedObject(self, &Keys.backgroundViewKey, view, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
NotificationCenter.default.addObserver(self, selector: #selector(popoverWillOpen(_:)), name: NSPopover.willShowNotification, object: nil)
return view
}
@objc private func popoverWillOpen(_ notification: Notification) {
if backgroundView.superview == nil {
if let contentView = contentViewController?.view, let frameView = contentView.superview {
frameView.wantsLayer = true
backgroundView.frame = NSInsetRect(frameView.frame, 1, 1)
backgroundView.autoresizingMask = [.width, .height]
frameView.addSubview(backgroundView, positioned: .below, relativeTo: contentView)
}
}
}
var backgroundColor: NSColor? {
get {
if let bgColor = backgroundView.layer?.backgroundColor {
return NSColor(cgColor: bgColor)
}
return nil
}
set {
backgroundView.wantsLayer = true
backgroundView.layer?.backgroundColor = newValue?.cgColor
}
}
}
Usage:
self.popover = popover
self.popover.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
这篇关于Swift ui mac os 菜单栏弹出框更改背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文