2
如果在指定contentView
的新尺寸的情況下如何調整NSWindow
的尺寸?根據內容的新尺寸調整NSWindow的尺寸查看
如果在指定contentView
的新尺寸的情況下如何調整NSWindow
的尺寸?根據內容的新尺寸調整NSWindow的尺寸查看
- (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];
}
你可以這樣做 -
[window setContentSize:newContentSize]
也有方法的窗口和內容大小之間的轉換:
- (NSRect)contentRectForFrameRect:(NSRect)windowFrame
- (NSRect)frameRectForContentRect:(NSRect)windowContent