NSView's autoresizing behavior

后端 未结 2 978
借酒劲吻你
借酒劲吻你 2020-12-28 12:00

I need to understand how NSView autoresizes it\'s views. I\'ve set everything up in IB and my subviews resize nicely (when I resize my window around with a mouse). However,

相关标签:
2条回答
  • 2020-12-28 12: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.

    0 讨论(0)
  • 2020-12-28 12:50

    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.

    0 讨论(0)
提交回复
热议问题