0

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)")
    }
}

enter image description here

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?

brianLikeApple
  • 4,262
  • 1
  • 27
  • 46
  • It sounds like your app is trying to rely on internal implementation details of `NSImageView` and my first instinct is to say “don’t do that”. That’s not very helpful, but as you’ve learned, Apple can and does change things in ways that can break your assumptions. Why does this change in behavior break your app? Can you find another way to accomplish what you’re trying to do? – Dave Weston Dec 04 '17 at 18:17
  • @DaveWeston is correct - you seem to be relying your software on internal implementation details of Apple. Hence your software may break whenever Apple changes its implementation. Unless you add some detail on what actually breaks in your software, its impossible you provide you further help. – mschmidt Dec 04 '17 at 22:45
  • @DaveWeston Thanks for your comment. But if you see the code I provided above, the custom view inherits from NSImageView which is eventually inherits from NSView. To me, NSImageView is nothing but special NSView and it should not add subview by itself! – brianLikeApple Dec 04 '17 at 23:13
  • 1
    There are lots of standard views that include other subviews. Unfortunately, for your purpose, `NSImageView` is now one of them. You could try changing Apple's mind by filing a radar, but I would say your odds are about 1 in a billion. :-) – Dave Weston Dec 05 '17 at 02:58
  • 1
    p.s. if you can provide more detail, I'm sure I or someone else on the side can hep you find a workaround. – Dave Weston Dec 05 '17 at 02:58
  • @DaveWeston I come here for reason. I think I know what is working around, just don't know why it is special...I am after some references that let me or us know what we should do :] – brianLikeApple Dec 05 '17 at 09:20

0 Answers0