Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to interact with layer-backed views on the Mac
    primarykey
    data
    text
    <p>I am designing a user interface containing several labels and text fields. I would like to style the UI like this:</p> <ol> <li>setting a background pattern for the content view of my <code>NSWindow</code></li> <li>adding a custom icon to the background in the upper left corner</li> </ol> <p>I solved the first problem by making the content view a <em>layer-backed view</em> as described in <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html" rel="nofollow noreferrer">Apple's documentation of <code>NSView</code></a>:</p> <blockquote> <p>A layer-backed view is a view that is backed by a Core Animation layer. Any drawing done by the view is the cached in the backing layer. You configured a layer-backed view by simply invoking <code>setWantsLayer:</code> with a value of <code>YES</code>. The view class will automatically create the a backing layer for you, and you use the view class’s drawing mechanisms. <strong>When using layer-backed views you should never interact directly with the layer.</strong></p> <p>A layer-hosting view is a view that contains a Core Animation layer that you intend to manipulate directly. You create a layer-hosting view by instantiating an instance of a Core Animation layer class and setting that layer using the view’s <code>setLayer:</code> method. After doing so, you then invoke <code>setWantsLayer:</code> with a value of <code>YES</code>. <strong>When using a layer-hosting view you should not rely on the view for drawing, nor should you add subviews to the layer-hosting view.</strong></p> </blockquote> <p>and then generating a <code>CGColorRef</code> out of a <code>CGPattern</code> which draws my <code>CGImage</code>:</p> <pre><code>NSView *mainView = [[self window]contentView]; [mainView setWantsLayer:YES]; </code></pre> <p>To set the background image as a pattern I used the answer from <a href="https://stackoverflow.com/questions/2520978/how-to-tile-the-contents-of-a-calayer">How to tile the contents of a CALayer</a> here on SO to get the first task done.</p> <p>However for the second task, adding the icon I used the code below:</p> <pre><code>CGImageRef iconImage = NULL; NSString *path = [[NSBundle mainBundle] pathForResource:@"icon_128" ofType:@"png"]; if(path != nil) { NSURL *imageURL = [NSURL fileURLWithPath:path]; provider = CGDataProviderCreateWithURL((CFURLRef)imageURL); iconImage = CGImageCreateWithPNGDataProvider(provider,NULL,FALSE,kCGRenderingIntentDefault); CFRelease(provider); } CALayer *iconLayer = [[CALayer alloc] init]; // layer is the mainView's layer CGRect layerFrame = layer.frame; CGFloat iconWidth = 128.f; iconLayer.frame = CGRectMake(0.f, CGRectGetHeight(layerFrame)-iconWidth, 128.f, 128.f); iconLayer.contents = (id)iconImage; CGImageRelease(iconImage); [layer insertSublayer:iconLayer atIndex:0]; [iconLayer release]; </code></pre> <h2>The Questions</h2> <ol> <li>I am not sure if I am violating Apple's restrictions concerning <em>layer-backed</em> views that you should never interact directly with the layer. When setting the layer's background color <em>I am</em> interacting directly with the layer or am I mistaken here?</li> <li>I have a bad feeling about interacting with the layer hierarchy of a <em>layer-backed</em> view directly and inserting a new layer like I did for my second task. Is this possible or also violating Apple's guidelines? I want to point out that this content view of course has several subviews such as labels, a text view and buttons.</li> <li>It seems to me that just using one single <em>layer-hosting</em> <code>NSView</code> seems to be the cleanest solution. All the text labels could then be added as <code>CATextLayers</code> etc. However if I understand Apple's documentation correctly I cannot add any controls to the view anymore. Would I have to code all the controls myself in custom <code>CALayers</code> to get it working? Sounds like reinventing the wheel <em>de luxe</em>. I also have no idea how one would code a <code>NSTextField</code> solely in CoreAnimation.</li> </ol> <p>Any advice on how split designing user interfaces with CoreAnimation and standard controls is appreciated.</p> <p><strong>Please note that I am talking about the Mac here.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload