有没有办法在 NSWindow 中隐藏标题栏?我不想完全编写一个新的自定义窗口。我不能使用 NSBorderlessWindowMask 因为我的窗口上有一个底栏,而使用 NSBorderlessWindowMask 会使它消失。我还尝试将 setContentBorderThickness:forEdge: 与 NSMaxYEdge 一起使用并将其设置为 0,但这也不起作用。
任何帮助表示赞赏
有没有办法在 NSWindow 中隐藏标题栏?我不想完全编写一个新的自定义窗口。我不能使用 NSBorderlessWindowMask 因为我的窗口上有一个底栏,而使用 NSBorderlessWindowMask 会使它消失。我还尝试将 setContentBorderThickness:forEdge: 与 NSMaxYEdge 一起使用并将其设置为 0,但这也不起作用。
任何帮助表示赞赏
[yourWindow setStyleMask:NSBorderlessWindowMask];
从 OS X 10.10 开始,您可以隐藏标题栏。
window1.titlebarAppearsTransparent = true
window1.titleVisibility = .Hidden
也许您想覆盖窗口样式。
window1.styleMask = NSResizableWindowMask
| NSTitledWindowMask
| NSFullSizeContentViewWindowMask
欢迎屏幕的种类NSWindow / NSViewController 设置(Swift 4.1)
extension NSWindow {
enum Style {
case welcome
}
convenience init(contentRect: CGRect, style: Style) {
switch style {
case .welcome:
let styleMask: NSWindow.StyleMask = [.closable, .titled, .fullSizeContentView]
self.init(contentRect: contentRect, styleMask: styleMask, backing: .buffered, defer: true)
titlebarAppearsTransparent = true
titleVisibility = .hidden
standardWindowButton(.zoomButton)?.isHidden = true
standardWindowButton(.miniaturizeButton)?.isHidden = true
}
}
}
class WelcomeWindowController: NSWindowController {
private (set) lazy var viewController = WelcomeViewController()
private let contentWindow: NSWindow
init() {
contentWindow = NSWindow(contentRect: CGRect(x: 400, y: 200, width: 800, height: 472), style: .welcome)
super.init(window: contentWindow)
let frameSize = contentWindow.contentRect(forFrameRect: contentWindow.frame).size
viewController.view.setFrameSize(frameSize)
contentWindow.contentViewController = viewController
}
}
class WelcomeViewController: NSViewController {
private lazy var contentView = View()
override func loadView() {
view = contentView
}
init() {
super.init(nibName: nil, bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
contentView.backgroundColor = .white
}
}
class View: NSView {
var backgroundColor: NSColor?
convenience init() {
self.init(frame: NSRect())
}
override func draw(_ dirtyRect: NSRect) {
if let backgroundColor = backgroundColor {
backgroundColor.setFill()
dirtyRect.fill()
} else {
super.draw(dirtyRect)
}
}
}
结果
我知道的唯一方法是创建一个没有标题栏的窗口(请参阅 NSBorderlessWindowMask)。请注意,您不能(轻松)在 IB 中创建没有标题栏的窗口,因此您必须在代码中做一些工作(有几种不同的方法,您可能会弄清楚)。
使用没有标题栏的窗口的一个很大的缺点是,您现在需要更多的标准外观和行为——圆角等。
我有一个经验,当我第一次设置窗口的内容视图然后设置窗口无边框时:
[yourWindow setStyleMask:NSBorderlessWindowMask];
我的窗口中不会出现任何内容。所以我首先设置了样式掩码,然后我设置了内容视图:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// 1. borderless window
[[self window] setStyleMask: NSBorderlessWindowMask];
// 2. create the master View Controller
self.masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
// 3. Add the view controller to the Window's content view
[self.window.contentView addSubview:self.masterViewController.view];
self.masterViewController.view.frame = ((NSView*)self.window.contentView).bounds;
}
瞧,我的窗口的内容已经出现了。
如果你得到关闭按钮的超级视图会发生什么?你能隐瞒吗?
// Imagine that 'self' is the NSWindow derived class
NSButton *miniaturizeButton = [self standardWindowButton:NSWindowMiniaturizeButton];
NSView* titleBarView = [miniaturizeButton superview];
[titleBarView setHidden:YES];
您可以使用GitHub 上可用的WAYInAppStoreWindow,它适用于 Yosemite 和 Mavericks。