简体   繁体   中英

Auto layout on subview inside subview (NSView)

I'm having hard time trying to properly set the auto layout for a subview inside another subview.

I'm using an example where two toolbar items show two different subviews (which works as expected), and those two share a third subview that's the one that does not fit well.

The code to add the subview is very simple:

[subView removeFromSuperview];
[itemXSubView addSubview:subView];
[self.window setContentView:itemXView];

First I remove the third and shared subview ( subView ) in case it was already added, then add it to the item[1-2]SubView and set the content of the window with the subview item[1-2]View , [1-2] depending on the toolbar button selected. Everything else is done with auto layout conditions.

The result is that the third and shared subview is always misplaced and/or cut, as in the example below. Resizing the window and changing from the first or the second view usually aggravates the issue.

Example of third subview items cut


Test updates

Tried to delegate the main window and override two resize functions (as per @the4kman suggestions), but they did never get called. The init is the only being called:

@interface viewController: NSView <NSWindowDelegate>
@end

@implementation viewController

-(id)init
{
   if((self=[super init])) { }
   return self;
}

- (void)resizeSubviewsWithOldSize:(NSSize)oldSize;
{
   [super resizeSubviewsWithOldSize:oldSize];
}

- (void)resizeWithOldSuperviewSize:(NSSize)oldSize;
{
   [super resizeWithOldSuperviewSize:oldSize];
}

- (void)layout
{
   [super layout];
}

Another suggestion that got called, but sadly with no actual improvement. Delegated the window to viewController and set the main view (self.view) to the nested subView. Tried also combining with [itemXSubView setNeedsLayout:true] ;:

@interface viewController: NSViewController <NSWindowDelegate>
@end


@implementation viewController

-(void)viewWillLayout
{
   [super viewDidLayout];
   [self.view setNeedsLayout:true];
}

@end

Thanks in advance!

itemXSubView resizes its subviews but it doesn't fit them. You have to fit subView inside item1SubView before addSubview .

- (IBAction)item1Action:(id)sender {
    NSLog(@"Item 1 action triggered");

    [subView removeFromSuperview];
    subView.frame = item1SubView.bounds;
    [item1SubView addSubview:subView];
    [self.window setContentView:item1View];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM