Hampus Tågerud Hampus Tågerud - 1 month ago 13
Objective-C Question

Using contentLayoutGuide for constraints

I´m trying to use a

NSTextView
with the new Vibrant theme in OSX 10.10 after watching the session from WWDC on advanced features of the new UI. My text view is not placed adjacent to the titlebar so I have made the content view fully sized so I can place the text view beneath title bar.

My problem is that I have a
NSView
between the text view and title bar which should be placed directly beneath the title bar but I can not get auto layout to do this for me. In the video they're giving this sample code using the new
contentLayoutGuide
property in
NSWindow
:

NSLayoutConstraint *topEdgeConstraint = [NSLayoutConstraint constraintWithItem:myView attribute: NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:window.contentLayoutGuide attribute:NSLayoutAttributeTop];


However I can not get this to work, whenever I try to install the constraint Xcode gives me


Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal.


I get that the window property used in the example is not in the subtree of the view since the view is placed inside the window but how am I supposed to access the
contentLayoutGuide
property?

Currently I´m trying to add the constraint in the
-awakeFromNib
method in a subclassed
NSWindow
. Wrong approach maybe?

Thanks in advance

Answer Source

Edit: this solution is no longer acceptable since OS X El Capitan which has a different views hierarchy in a window frame. Look for the sam's answer where he suggests to use a new NSLayoutConstraint methods and properties (constraints activation) that were introduced in Yosemite.

I've faced the same issue yesterday