viewとNSView,NSWindowの関係や役割がイマイチわかりません
NSWindowでウィンドウの設定をしたらウィンドウが二つ出てきました
一つは自分で呼び出したNSView(frame: NSRect( ... ))の引数通りのウィンドウ
もう一つはNSWindowでウィンドウの設定をしたウィンドウです
なぜこのようなことが起こったのでしょうか?
ちなみにコードでは
Swift
1myView = NSView(frame: NSRect(x: 0, y: 0, width: 800, height: 200 ) ) 2と 3self.View = NSView(frame: NSRect(x: 0, y: 0, width: 1500, height: 200) )
という書き方もしています(関係があるかわかりませんが)
あとは下記の参考にしたコードもある。
自分のプログラム上の原因ではなく上記のことが起こりうる原因が知りたいので
コードは記載しませんが答えられるところまででいいのでよろしくお願いします。
(いじりすぎてごちゃごちゃなので恥ずかしい...)
参考にしたコード
https://joyplot.com/documents/2018/06/24/swift-nswindow-no-xib/
これ自体はできましたが
これを応用しようとしたら上記の自体に陥りました。
補足
思ったより簡単に再現できたので
コードを載せます
Swift
1import Cocoa 2import MetalKit 3 4class ViewController: NSViewController { 5 6 override func loadView() { 7 self.view = NSView(frame: NSRect(x: 0, y: 0, width: 300, height: 200)) 8 } 9}
swift
1 2import Cocoa 3 4@NSApplicationMain 5class AppDelegate: NSObject, NSApplicationDelegate { 6 7// private var meinWindow: NSWindow! = NSWindow(contentViewController: ViewController()) 8 9 var window: NSWindow! 10 11 func applicationDidFinishLaunching(_ aNotification: Notification) { 12 window.makeKeyAndOrderFront(nil) 13 } 14 15 func applicationWillFinishLaunching(_ notification: Notification) { 16 17 // ウィンドウの作成 18 window = NSWindow(contentViewController: ViewController()) 19 window.title = "New Window" 20 window.isOpaque = false 21 window.backgroundColor = NSColor(white: 1, alpha: 0.5) 22 window.center() 23 } 24 25 // ウィンドウが閉じられたらアプリも終了する 26 func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { 27 return true 28 } 29} 30
あなたの回答
tips
プレビュー