2011-03-07 54 views
0

我正在使用this Cocoa with Love文章中显示的方法来创建自定义NSWindow子类。就像在这个例子中,为了绘制一个箭头(我正在创建一个弹出式窗口),我需要在窗口内容周围留出大约10px的边距。我必须在整个窗口周围留有余量,而不是只有带有箭头的一侧,因为我希望能够更改箭头位置而不必重新定位内容。自定义NSWindow内容边缘导致自动调整面具

总之,我使用要做到这一点的方法是(相关代码在底部):

  • 覆盖NSWindow的contentRectForFrameRect:frameRectForContentRect:styleMask:方法来添加内容周围的填充:
  • 将窗口的自定义绘制框架视图设置为contentView,然后覆盖contentView的setter和getter,以便将传入的视图添加为框架视图的子视图

的问题是,意见的自动尺寸口罩内窗口的实际内容查看完全搞砸了。下面是我如何设置界面生成器中的内容:

Layout

这里是如何表视图滚动视图的自动尺寸调整掩码设置:

Table View Autoresizing

而且这里是如何文本标签的自动尺寸调整掩码设置:

Label Autoresizing

而这里的结果是什么样的应用程式:

Actual result

相关代码(从上述文章中得出)

#define CONTENT_MARGIN 10.0 

- (NSRect)contentRectForFrameRect:(NSRect)windowFrame 
{ 
    windowFrame.origin = NSZeroPoint; 
    return NSInsetRect(windowFrame, CONTENT_MARGIN, ICONTENT_MARGIN); 
} 

- (NSRect)frameRectForContentRect:(NSRect)contentRect 
{ 
    return NSInsetRect(contentRect, -CONTENT_MARGINT, -CONTENT_MARGIN); 
} 

+ (NSRect)frameRectForContentRect:(NSRect)contentRect 
         styleMask:(NSUInteger)windowStyle 
{ 
    return NSInsetRect(contentRect, -CONTENT_MARGIN, -CONTENT_MARGIN); 
} 


- (NSView*)contentView 
{ 
    return _popoverContentView; 
} 

- (void)setContentView:(NSView *)aView 
{ 
    if ([_popoverContentView isEqualTo:aView]) { return; } 
    NSRect bounds = [self frame]; 
    bounds.origin = NSZeroPoint; 
    SearchPopoverWindowFrame *frameView = [super contentView]; 
    if (!frameView) { 
     frameView = [[[SearchPopoverWindowFrame alloc] initWithFrame:bounds] autorelease]; 
     [super setContentView:frameView]; 
    } 
    if (_popoverContentView) { 
     [_popoverContentView removeFromSuperview]; 
    } 
    _popoverContentView = aView; 
    [_popoverContentView setFrame:[self contentRectForFrameRect:bounds]]; 
    [_popoverContentView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)]; 
    [frameView addSubview:_popoverContentView]; 
} 

我想,也许正想过的酥料饼的内容不知何故,所以我画了一个内容视图边框,但不是,一切都应该是。唯一的问题是内容视图中标签和表格视图的自动识别蒙版不能像他们应该那样工作。任何意见是极大的赞赏。

编辑:如果任何人有兴趣,我已经在github上打开了这个弹出窗口/控制器的完整代码,如INPopoverController。包含一个示例项目,以防您尝试重现问题。

+0

Xcode 4目前处于NDA下。你可以向我们展示截图吗? – 2011-03-07 02:40:47

+0

我已经删除/替换了显示Xcode 4的部分,但我确定每个人都知道它现在的样子。 – indragie 2011-03-07 02:47:57

+0

您的表视图的滚动视图应该在自动大小检查器中连接的底边上有支撑。 – 2011-03-07 05:53:54

回答

1
-(void)scaleWindowForHeight:(float)height 
{ 
    if (height > 22) 
    { 
     NSWindow* window = [self window]; 
     NSRect old_window_frame = [window frame]; 
     NSRect old_content_rect = [window contentRectForFrameRect: old_window_frame]; 
     NSSize new_content_size = NSMakeSize(old_window_frame.size.width, height); 
     // need to move window by Y-axis because NSWindow origin point is at lower side: 
     NSRect new_content_rect = NSMakeRect(NSMinX(old_content_rect), NSMaxY(old_content_rect) - new_content_size.height, new_content_size.width, new_content_size.height); 
     NSRect new_window_frame = [window frameRectForContentRect: new_content_rect]; 
     [window setFrame: new_window_frame display:YES animate: [window isVisible] ]; 
    } 
    else 
     NSLog(@"window size too small"); 
}