NSView Layout Method Not Called After SetNeedsLayout: YES

Like OSX 10.7, if you want to manually build the NSView, you must do this by overriding the layout method and whenever you plan to call this method, you simply do:

 [myView setNeedsLayout:YES] 

I am very familiar with this template in iOS, however on OSX it does not seem to work. I created a custom NSView and implemented layout , but it seems like it is never called. Ever. Not after adding subviews, not after calling setNeedsLayout:YES , never, and I don't know why. I can manually call layout , and everything works as expected, but the docs say never to do this.

From Xcode:

 - (void)layout Override this method if your custom view needs to perform custom layout not expressible using the constraint-based layout system. In this case you are responsible for calling setNeedsLayout: when something that impacts your custom layout changes. 

From online docs:

You should only redefine the layout only if you want to execute your own layout. If you do this, you also need to call setNeedsLayout: when something fits into your custom layout changes.

Link: http://developer.apple.com/library/mac/#releasenotes/UserExperience/RNAutomaticLayout/#//apple_ref/doc/uid/TP40010631-CH1-SW14

Any help is greatly appreciated.

UPDATE: Here is a link to a sample Xcode project that illustrates the LayoutTest.zip problem

+4
source share
1 answer

You need to enable autorun. If you are using an xib file (and you must not be a commando!), You can check the auto-detection flag in the Document Builder Document in the file inspector.

In code: [myView setTranslatesAutoresizingMaskIntoConstraints: YES]

In your example project, you have no limits. The view will not invoke the layout without any restrictions.

 NSView *aView = self.window.contentView; NSDictionary *viewsDictionary=NSDictionaryOfVariableBindings(aView, complexView); NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"[aView]-[complexView]" options:0 metrics:nil views:viewsDictionary]; for (NSLayoutConstraint *constraint in constraints) { [complexView addConstraint:constraint]; } 
+2
source

Source: https://habr.com/ru/post/1487099/


All Articles