Add CATextLayer on Top of NSImageView
You needs explicitly say NSImageView
wantsLayer
before use layers, like below
self.img_view.wantsLayer = true
let myTextLayer = CATextLayer()
myTextLayer.string = "My text"
myTextLayer.foregroundColor = NSColor.cyan.cgColor
myTextLayer.frame = self.img_view.bounds
self.img_view.layer.addSublayer(myTextLayer)
NSImageView with CALayer on Top
Thanks to mohacs, he/she answered the question in this comment ―
you can try changing z order of the layer. http://stackoverflow.com/questions/25371109/z-index-of-image-and-separator-in-uitableviewcell/25372394#25372394 – mohacs Aug 30 '14 at 21:04
Displaying several layered images into an NSView
I figured it out: the problem is that I needed to specify a frame for my sublayers
@IBOutlet weak var mapImageView: NSImageView!
@IBOutlet weak var countriesView: NSView!
override func viewDidLoad() {
super.viewDidLoad()
mapImageView.image = NSImage(named: "world")?.tint(color: .white)
countriesView.wantsLayer = true
let usLayer = CALayer()
usLayer.contentsGravity = .resizeAspect
usLayer.contents = NSImage(named: "US")
usLayer.frame = countriesView!.layer!.frame
countriesView?.layer?.addSublayer(usLayer)
}
Annotating over an NSImageView with CALayers, layer-hosting or layer-backed?
The solution I went for was to not alter the NSImageView subclass's layer at all. Instead I added a subview to the NSImageView subclass, constrained the view such that is has the same bounds as the parent, and then made this subview layer-hosting. It is then possible to add sublayer to this 'annotation' view's layer. In this way I can annotate on top of an NSImageView.
How to bring an NSImageView to the front or back of another NSImageView?
What you are asking about, I think, is how to control the z-order of views.
For historical performance reasons this is not well supported in AppKit (unlike UIKit in iOS), since until somewhat recently you couldn't actually have sibling views that overlap.
A common approach to this (on recent OS X releases) is to use Core Animation (in particular, CALayer
) which does support z-ordering natively, but this is probably overkill for what you need (and in any event is going to have a learning curve for you).
What are you actually trying to do? Are these images (image views) precisely on top of one another? If so, the easiest (and much better performing) approach is to have a single NSImageView
and to just send -setImage:...
to it to change the displayed image.
NSImageView image aspect fill?
You may find it much easier to subclass NSView
and provide a CALayer
that does the aspect fill for you. Here is what the init
might look like for this NSView
subclass.
- (id)initWithFrame:(NSRect)frame andImage:(NSImage*)image
{
self = [super initWithFrame:frame];
if (self) {
self.layer = [[CALayer alloc] init];
self.layer.contentsGravity = kCAGravityResizeAspectFill;
self.layer.contents = image;
self.wantsLayer = YES;
}
return self;
}
Note that the order of setting the layer, then settings wantsLayer is very important (if you set wantsLayer first, you'll get a default backing layer instead).
You could have a setImage
method that simply updates the contents of the layer.
Related Topics
Drawing at Cocoa with Swift Creates an Error
Aws Cognito Credentialsprovider.Login Always Shows Nil (Swift)
Force Refresh on Another Viewcontroller Component with Swift
Using Cfarraygetvalueatindex in Swift with Unsafepointer (Aupreset)
Adding Items to The Dock Menu from My View Controller in My Cocoa App
How to Add UIpickerview in UIalertcontroller
Firebase Swift 3 Get List of Child in a Array
Xcode 9.3 Watchkit Crash on Wkinterfacebutton Tap
List Inside Scrollview Is Not Displayed on Watchos
How to Display an Alert Controller When Nsdate == to a Time (For Example 12:00 Am) in Swift
Swift Method Parameters in Documentation
How to Insert a Row in Tableview Without Using Reloaddata Function in Cocoa
Update Widget When Appearance Changes
Dynamictype of Optional Chaining Not The Same as Assignment
Parse Weird Bug in Swift That Causes Acl Write Permissions to Change to an Objectid
How to Set Default Clouse Param in View Method
<Unknown>:0: Error: Opening Import File for Module 'swift': Not a Directory