NSImageView在High Sierra上表现不正常

时间:2017-12-04 15:48:17

标签: swift macos cocoa macos-high-sierra

我只对High Sierra有一个奇怪的问题NSImageView。我的软件在以前的macOS上运行得很好,直到我更新到最新版本的macOS。

所以我详细了解了这一点,发现奇怪的是,NSImageView正在添加NSImageViewContainerView作为子视图,您无法在线找到任何相关信息......

为了清楚说明,我已经为您的理解创建了一个非常简单的演示。只需将以下代码复制到新创建的项目中即可。

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

从日志中,您可以清楚地看到NSImageViewContainerView添加MyCustomView作为子视图,这基本上会导致我的软件出现问题。如果您将NSImageView更改为NSView,那么您将无法看到我对代码所期望的任何子视图。

我无法查看有关NSImageView的任何相关信息,我在这里缺少什么?

0 个答案:

没有答案