INAppStoreWindow
是一个NSWindow子类,最初开发用于模仿OS X 10.6.6中引入的Mac App Store应用中的主窗口外观。
自从MAS应用从这种设计过渡以来,但INAppStoreWindow
仍在积极开发,以提供对NSWindow
标题栏的广泛自定义选项。
INAppStoreWindow的功能
使用INAppStoreWindow
与更改Interface Builder中NSWindow
的类一样简单,或者简单地在代码中创建一个INAppStoreWindow
实例(如果您是程序化执行)。我已经包括一个示例项目,演示如何使用INAppStoreWindow
。
注意:默认情况下,标题栏高度设置为标准窗口标题高度。您必须设置'titleBarHeight'属性才能增加标题栏的高度。
似乎有些人遇到了一个问题,即当在未将NSWindow类型转换为INAppStoreWindow类的情况下调用方法时,标题栏高度属性设置不正确。如果您遇到此问题,请执行以下操作(例如使用窗口控制器)
INAppStoreWindow *aWindow = (INAppStoreWindow*)[windowController window];
aWindow.titleBarHeight = 60.0;
将控件和其他视图添加到标题栏很简单。这可以通过程序化或通过Interface Builder来完成。以下是两种方法的示例
程序化
// This code places a 100x100 button in the center of the title bar view
NSView *titleBarView = self.window.titleBarView;
NSSize buttonSize = NSMakeSize(100.f, 100.f);
NSRect buttonFrame = NSMakeRect(NSMidX(titleBarView.bounds) - (buttonSize.width / 2.f), NSMidY(titleBarView.bounds) - (buttonSize.height / 2.f), buttonSize.width, buttonSize.height);
NSButton *button = [[NSButton alloc] initWithFrame:buttonFrame];
[button setTitle:@"A Button"];
[titleBarView addSubview:button];
Interface Builder
注意:尽管可以在Interface Builder中设置标题栏的内容布局,您仍然需要使用以下代码来显示在IB中创建的视图在标题栏中。
// self.titleView is a an IBOutlet to an NSView that has been configured in IB with everything you want in the title bar
self.titleView.frame = self.window.titleBarView.bounds;
self.titleView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[self.window.titleBarView addSubview:self.titleView];
可以通过两个属性来控制交通灯和全屏按钮的垂直居中:centerTrafficLightButtons
和centerFullScreenButton
。
交通灯按钮默认垂直居中。
您可以使INAppStoreWindow在全屏模式下隐藏,并在退出时重新出现。只需设置属性hideTitleBarInFullScreen
以隐藏即可。
交通灯的左侧填充可以使用trafficLightButtonsLeftMargin
进行调整,全屏按钮的右侧填充可以使用fullScreenButtonRightMargin
进行调整。
可以通过设置showsBaselineSeparator
为NO
来隐藏基线分隔,默认值为YES
。
为了自定义这些按钮,您将使用INWindowButton
类。您必须为每个按钮创建一个单独的实例,并为按钮的每个状态提供您的图形。目前支持以下状态
请参阅INWindowButton.h
头文件文档以获取更多详细信息。
您可以通过将showsTitle
属性设置为YES
来启用标题绘制。对于基于NSDocument的应用,您可以通过将showsDocumentProxyIcon
属性设置为YES
来启用绘制文档代理图标。您可以使用titleTextColor
、inactiveTitleTextColor
、titleTextShadow
和inactiveTitleTextShadow
属性调整外观。您还可以通过设置showsTitleInFullscreen
属性为YES
来在全屏模式下启用标题绘制。
我们投入了大量的时间和精力来让INAppStoreWindow的自定义标题栏功能工作得恰到好处,因此重新实现所有这些工作来绘制您自己的自定义标题栏将是令人遗憾的。因此,INAppStoreWindow有一个titleBarDrawingBlock
属性,可以将其设置为包含您自己的绘图代码的块!
[self.window setTitleBarDrawingBlock:^(BOOL drawsAsMainWindow, CGRect drawingRect, CGPathRef clippingPath){
// Custom drawing code!
}];
这个块会传递一些有用的参数,如是否为主窗口(drawsAsMainWindow
)、标题栏的绘制矩形(drawingRect
)以及具有顶部圆角的预先制作的裁剪路径(clippingPath
)。
如果您只需要调整标题栏的颜色而无需自己绘制整个内容,有一些属性可以帮助您做到这一点
self.window.titleBarStartColor = [NSColor colorWithCalibratedWhite: 0.6 alpha: 1.0];
self.window.titleBarEndColor = [NSColor colorWithCalibratedWhite: 0.9 alpha: 1.0];
self.window.baselineSeparatorColor = [NSColor colorWithCalibratedWhite: 0.2 alpha: 1.0];
self.window.inactiveTitleBarEndColor = [NSColor colorWithCalibratedWhite: 0.95 alpha: 1.0];
self.window.inactiveTitleBarStartColor = [NSColor colorWithCalibratedWhite: 0.8 alpha: 1.0];
self.window.inactiveBaselineSeparatorColor = [NSColor colorWithCalibratedWhite: 0.4 alpha: 1.0];
INAppStoreWindow
的附加扩展提供在扩展文件夹中。
将这个类别添加到您的项目中,以修复对基于文档的应用的标题栏布局,以响应-[NSDocument updateChangeCount:]
。这个修复被从主INAppStoreWindow
代码库中分离出来,因为它在NSDocument
上翻转了方法。
INAppStoreWindow 由 Indragie Karunaratne 和 David Keegan 维护。特别感谢 其他所有人 为代码提供了各种修复和改进。
INAppStoreWindow 遵循 BSD 许可证。