我正在开发一个主窗口有NSDrawer的应用。抽屉始终在右边缘打开是至关重要的,这就是我将它编码为工作的方式。我想知道的是,是否有办法检测抽屉是否会“从屏幕上”打开......有没有办法可以检测到这个?如果是这样,怎么样?另外,如何移动主窗口以调整将打开的抽屉的宽度?
提前致谢。
尼克
编辑:
感谢Rob的建议,这是解决方案。
-(IBAction)toggleDrawer:(id)sender
{
NSRect screenFrame = [[[NSScreen screens] objectAtIndex:0] visibleFrame];
NSRect windowFrame = [window frame];
NSRect drawerFrame = [[[drawer contentView] window] frame];
if ([drawer state] == NSDrawerOpenState)
{
[drawer close];
}
else
{
if (windowFrame.size.width +
windowFrame.origin.x +
drawerFrame.size.width > screenFrame.size.width)
{
NSLog(@"Will Open Off Screen");
float offset = (windowFrame.size.width +
windowFrame.origin.x +
drawerFrame.size.width) - screenFrame.size.width;
NSRect newRect = NSMakeRect(windowFrame.origin.x - offset,
windowFrame.origin.y,
windowFrame.size.width,
windowFrame.size.height);
[window setFrame:newRect display:YES animate:YES];
}
[drawer openOnEdge:NSMaxXEdge];
}
}
答案 0 :(得分:4)
您可以使用NSScreen的方法来计算扩展抽屉的框架是否会在屏幕外打开,然后使用-setFrame:display:animate:在打开窗口之前将窗口移离屏幕边缘必要的距离抽屉。