这段代码
NSView* superView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
NSView* subView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
[superView addSubview:subView];
[superView setAutoresizesSubviews:YES];
[subView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
NSLog(@"subview's frame before resizing: %@", NSStringFromRect([subView frame]));
[superView setFrame:NSMakeRect(0, 0, 200, 100)];
NSLog(@"subview's frame after resizing: %@", NSStringFromRect([subView frame]));
确实给出了预期的结果:
[...] subview's frame before resizing: {{0, 0}, {100, 100}}
[...] subview's frame after resizing: {{0, 0}, {200, 100}}
你的问题在别处。我希望在调整大小时,您的容器视图之一不是窗口视图层次结构的一部分。
解决您的问题的一个好的解决方案可能是使用不带选项卡的 NSTabView,因为这样您的所有视图始终都在窗口中(可能隐藏),并且选项卡视图负责调整大小。
另一种解决方案是将多个视图放在彼此之上并使用setHidden:
仅显示一个。