I have a strange issue for NSImageView
only on High Sierra. My software was working at previous macOS very well until I updated to latest version of macOS.
So I have looked at this in detail and found that strangely, NSImageView
is adding NSImageViewContainerView
as a subview which you cannot find any information about it from online...
To make my point clear, I have created a very simple demo for your understanding. Simply copy the following code to your newly created project.
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
let imageView = MyCustomView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.wantsLayer = true
imageView.layer?.backgroundColor = NSColor.red.cgColor
self.window.contentView?.addSubview(imageView)
self.window.contentView?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[imageView]|", options: [], metrics: nil, views: ["imageView": imageView]))
self.window.contentView?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[imageView]|", options: [], metrics: nil, views: ["imageView": imageView]))
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
class MyCustomView: NSImageView {
override func mouseUp(with event: NSEvent) {
print("This NSImageView\(self) has: \(self.subviews.count) subview(s) \(self.subviews)")
}
}
From the log, you can clearly see there is NSImageViewContainerView
added to MyCustomView
as a subview which basically cause the issue for my software. If you change NSImageView
to NSView
, then you cannot see any subviews which is what I expected from the code.
I am not able to check any related information about NSImageView
, is there anything I am missing here?