如何在 NSMenu 中放置水平滑块 (Swift 3, Xcode 8)
How to place Horizontal Slider in NSMenu (Swift 3, Xcode 8)
从 macOS Sierra 开始,音量菜单栏项提供了一个水平滑块项来更改系统的音量:
我想在我自己的应用程序中采用这个概念,并提出了以下 class:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
let statusItem = NSStatusBar.system().statusItem(withLength: -2)
func applicationDidFinishLaunching(_ aNotification: Notification) {
let menu = NSMenu()
let menuItem = NSMenuItem()
let statusSlider = NSSlider()
menu.addItem(NSMenuItem(title: "Slider:", action: nil, keyEquivalent: ""))
menuItem.title = "Slider 1"
menuItem.view = statusSlider
menu.addItem(menuItem)
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(title: "Quit", action: Selector("terminate:"), keyEquivalent: "q"))
statusItem.image = NSImage(named: "NSStatusAvailable")
statusItem.menu = menu
}
}
但是菜单中没有显示滑块。有人知道我做错了什么吗?
NSSlider 的初始帧大小为零。您需要在将其添加到菜单项之前设置大小。
statusSlider.setFrameSize(NSSize(width: 160, height: 16))
从 macOS Sierra 开始,音量菜单栏项提供了一个水平滑块项来更改系统的音量:
我想在我自己的应用程序中采用这个概念,并提出了以下 class:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
let statusItem = NSStatusBar.system().statusItem(withLength: -2)
func applicationDidFinishLaunching(_ aNotification: Notification) {
let menu = NSMenu()
let menuItem = NSMenuItem()
let statusSlider = NSSlider()
menu.addItem(NSMenuItem(title: "Slider:", action: nil, keyEquivalent: ""))
menuItem.title = "Slider 1"
menuItem.view = statusSlider
menu.addItem(menuItem)
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(title: "Quit", action: Selector("terminate:"), keyEquivalent: "q"))
statusItem.image = NSImage(named: "NSStatusAvailable")
statusItem.menu = menu
}
}
但是菜单中没有显示滑块。有人知道我做错了什么吗?
NSSlider 的初始帧大小为零。您需要在将其添加到菜单项之前设置大小。
statusSlider.setFrameSize(NSSize(width: 160, height: 16))