NSView's autoresizing behavior

喜你入骨 提交于 2019-11-30 02:29:47

This code

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]));

does give the expected result:

[...] subview's frame before resizing: {{0, 0}, {100, 100}}
[...] subview's frame after  resizing: {{0, 0}, {200, 100}}

Your problem is elsewhere. I expect that one of your container views is not a part of the view hierarchy of the window at the time when the resize occurs.

A good solution for your problem might be the use an NSTabView without tabs, because then all your views are at all times in the window (possibly hidden) and the tabview takes care of the resizing.

Another solution is to put several views on top of each other and use setHidden: to show only one.

Turns out my approach is correct. I was setting the frame size to an incorrect value, thus I wasn't getting the expected behavior. Doh.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!