有没有方法可以隐藏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
window.styleMask = [.resizable, .titled, .closable, .miniaturizable, .fullSizeContentView]
- endavidSwift 4.1中的欢迎界面NSWindow / NSViewController设置方法
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)
}
}
}
结果
// Imagine that 'self' is the NSWindow derived class
NSButton *miniaturizeButton = [self standardWindowButton:NSWindowMiniaturizeButton];
NSView* titleBarView = [miniaturizeButton superview];
[titleBarView setHidden:YES];
我有这样的经历:当我第一次设置窗口的内容视图,然后将窗口设置为无边框时:
[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;
}
瞧,我的窗口内容已经出现了。
我知道的唯一方法是创建一个没有标题栏的窗口(参见NSBorderlessWindowMask)。请注意,您不能在IB中轻松地创建没有标题栏的窗口,因此您需要在代码中进行一些工作(有几种不同的方法,您可能可以弄清楚)。
使用没有标题栏的窗口的一个大缺点是,您现在需要处理更多标准外观和行为 - 圆角等。