在第二个 ViewController 中禁用 closeButton

Disable closeButton in 2nd ViewController

我试图在我的第二个 ViewController "OptionsVC"

中禁用关闭按钮

这是我尝试过的:

self.view.window!.standardWindowButton(NSWindowButton.closeButton)!.isHidden = true

做到了,得到了这个: EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0

也试过

var button = view.window?.standardWindowButton(NSWindowButton.ZoomButton)
button?.isEnabled = false

无变化

为了禁用按钮,应该对其进行初始化。因此,您将面临从另一个视图控制器禁用按钮的问题。尝试添加一个布尔值并在 viewDidLoad() 上检查布尔值并决定启用或禁用按钮。

我知道了。

override func viewDidAppear(){
super.viewDidAppear()

if let window1 = self.view.window
  {
    window1.styleMask.remove( [.closable, .resizable] )
  }
}

这解决了我的问题