我希望完成完全相同的事情。我可以通过在WAYAppStoreWindow(或 INAppStoreWindow)中设置标题栏视图的超级视图来发布帧更改通知,然后在此视图上观察帧更改通知来做到这一点。在您的设置中添加以下观察者:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowTitleBarFrameDidChange:)
name:NSViewFrameDidChangeNotification
object:window.titleBarView.superview];
并在进入/退出全屏时打开/关闭帧更改通知:
window.titleBarView.superview.postsFrameChangedNotifications = YES;
然后在windowTitleBarFrameWillChange:
方法的实现中,通过比较它的superview框架的最后一个y位置来检查标题栏何时显示。这是代码:
// Window titlebar heights.
#define kWindowTitlebarHeightDefault 22.0
#define kWindowTitlebarHeightStandard 37.0
#define kWindowTitlebarHeightExtended 82.0
- (void) windowTitleBarFrameDidChange:(NSNotification *)notification;
{
NSView * titleBarView = [notification object]; // view is actually the titlebar container view
if (NSMinX(_lastTitleBarFrame) == NSMinX(titleBarView.frame) &&
NSWidth(_lastTitleBarFrame) == NSWidth(titleBarView.frame) &&
NSHeight(_lastTitleBarFrame) == NSHeight(titleBarView.frame) &&
NSMinY(_lastTitleBarFrame) == -kWindowTitlebarHeightDefault &&
NSMinY(titleBarView.frame) > -kWindowTitlebarHeightDefault) // titlebar will show
{
[self windowTitleBarWillShow];
}
else if (NSMinX(_lastTitleBarFrame) == NSMinX(titleBarView.frame) &&
NSWidth(_lastTitleBarFrame) == NSWidth(titleBarView.frame) &&
NSHeight(_lastTitleBarFrame) == NSHeight(titleBarView.frame) &&
NSMinY(_lastTitleBarFrame) == 0.0 && NSMinY(titleBarView.frame) < 0.0) // titlebar will hide
{
[self windowTitleBarWillHide:YES];
}
else if (NSWidth(_lastTitleBarFrame) != NSWidth([NSScreen mainScreen].frame) &&
NSWidth(titleBarView.frame) == NSWidth([NSScreen mainScreen].frame)) // just went full-screen
{
[self windowTitleBarWillHide:NO];
}
_lastTitleBarFrame = titleBarView.frame;
}
- (void) windowTitleBarWillHide:(BOOL)animate
{
WAYAppStoreWindow * window = (WAYAppStoreWindow *)[(NSWindowController *)[[self windowControllers] objectAtIndex:0] window];
NSView * themeFrame = window.titleBarView.superview.superview;
if (animate)
[themeFrame animator].alphaValue = 0.0;
else
themeFrame.alphaValue = 0.0;
}
- (void) windowTitleBarWillShow
{
WAYAppStoreWindow * window = (WAYAppStoreWindow *)[(NSWindowController *)[[self windowControllers] objectAtIndex:0] window];
NSView * themeFrame = window.titleBarView.superview.superview;
[themeFrame animator].alphaValue = 1.0;
}
- (void) windowWillEnterFullScreen:(NSNotification *)notification
{
WAYAppStoreWindow * window = [notification object];
[self setUpFullScreenTitleBarForWindow:window];
_lastTitleBarFrame = NSZeroRect;
}
- (void) windowDidEnterFullScreen:(NSNotification *)notification;
{
WAYAppStoreWindow * window = [notification object];
window.titleBarView.superview.postsFrameChangedNotifications = YES;
_fullscreenToolbarView.hidden = NO;
}
- (void) windowWillExitFullScreen:(NSNotification *)notification;
{
WAYAppStoreWindow * window = [notification object];
window.titleBarView.superview.postsFrameChangedNotifications = NO;
[self setUpStandardTitleBarForWindow:window];
}
- (void) setUpNormalTitleBarForWindow:(WAYAppStoreWindow *)window
{
window.appearance = nil;
window.showsTitle = NO;
window.titleBarHeight = kWindowTitlebarHeightExtended;
window.verticalTrafficLightButtons = YES;
window.centerTrafficLightButtons = YES;
window.trafficLightButtonsLeftMargin = 13.0;
_fullscreenToolbarView.hidden = YES;
}
- (void) setUpFullScreenTitleBarForWindow:(WAYAppStoreWindow *)window
{
window.appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantDark];
window.showsTitle = YES;
window.titleBarHeight = kWindowTitlebarHeightStandard;
window.verticalTrafficLightButtons = NO;
window.centerTrafficLightButtons = YES;
window.trafficLightButtonsLeftMargin = 13.0;
window.verticallyCenterTitle = YES;
_fullscreenToolbarView.hidden = YES;
_lastTitleBarFrame = NSZeroRect;
}
注意:我正在使用WAYWindow的一个分支,它增加了对在标题栏中垂直居中文档标题的支持。