给出contentView的新大小,调整NSWindow的大小

时间:2012-08-17 15:49:59

标签: macos cocoa nswindow

考虑到NSWindow的新尺寸,您如何调整contentView的尺寸?

2 个答案:

答案 0 :(得分:4)

- (void) resizeWindowWithContentSize:(NSSize)contentSize animated:(BOOL)animated {
    CGFloat titleBarHeight = self.window.frame.size.height - ((NSView*)self.window.contentView).frame.size.height;
    CGSize windowSize = CGSizeMake(contentSize.width, contentSize.height + titleBarHeight);

    // Optional: keep it centered
    float originX = self.window.frame.origin.x + (self.window.frame.size.width - windowSize.width) / 2;
    float originY = self.window.frame.origin.y + (self.window.frame.size.height - windowSize.height) / 2;
    NSRect windowFrame = CGRectMake(originX, originY, windowSize.width, windowSize.height);

    [self.window setFrame:windowFrame display:YES animate:animated];
}

答案 1 :(得分:2)

你可以做 -

[window setContentSize:newContentSize]

还有一些方法可以在窗口和内容大小之间进行转换:

  • - (NSRect)contentRectForFrameRect:(NSRect)windowFrame
  • - (NSRect)frameRectForContentRect:(NSRect)windowContent