如何禁用关闭按钮?

时间:2016-01-19 20:14:25

标签: swift macos cocoa

我需要一些帮助来弄清楚如何在OS X Cocoa和Swift 2中禁用/隐藏关闭,最小化和调整大小按钮。这是我尝试过的代码。我知道这是标题栏,但我想我还是会尝试一下:

self.window.titleVisibility = NSWindowTitleVisibility.Hidden;

有人知道怎么做吗?我正在使用Swift 2,OS X Cocoa和Xcode 7.2。谢谢!

7 个答案:

答案 0 :(得分:7)

也试试;

    self.window!.standardWindowButton(NSWindow.ButtonType.closeButton)!.hidden = true
    self.window!.standardWindowButton(NSWindow.ButtonType.miniaturizeButton)!.hidden = true

答案 1 :(得分:3)

在Xcode 9.1中,您可以在ViewController中使用以下内容,

from tkinter import *

class Login_sys(Frame):
    def __init__(self,master):
        super(Login_sys,self).__init__(master)
        self.userlist=['user1','user2']
        self.passlist=['pass1','pass2']
        self.grid()
        self.initial()
    def initial(self):
        self.CurrentError=('')
        self.user_E=StringVar()
        self.user_E.set('')
        self.pass_E=StringVar()
        self.pass_E.set('')
        self.title_lbl=Label(self,text='Welcome',fg='blue')
        self.title_lbl.grid(row=0,column=0,columnspan=1,sticky=W)
        self.login_bttn=Button(self,text='  Log In  ',fg='red',command=self.login)
        self.login_bttn.grid(row=1,column=0,columnspan=1,sticky=W)
        self.reg_bttn=Button(self,text='  Register  ',fg='red',command=self.register)
        self.reg_bttn.grid(row=2,column=0,columnspan=1,sticky=W)
    def login(self):
        self.title_lbl.destroy()
        self.login_bttn.destroy()
        self.reg_bttn.destroy()
        self.LI_title_lbl=Label(self,text='Log In',fg='blue')
        self.LI_title_lbl.grid(row=0,column=0,columnspan=1,sticky=W)
        self.user_lbl=Label(self,text='Username:',fg='red')
        self.user_lbl.grid(row=1,column=0,columnspan=1,sticky=W)
        self.userE=Entry(self,text=self.user_E)
        self.userE.grid(row=1,column=1,columnspan=1,sticky=W)
        self.pass_lbl=Label(self,text='Password:',fg='red')
        self.pass_lbl.grid(row=2,column=0,columnspan=1,sticky=W)
        self.passE=Entry(self,show='*')
        self.passE.grid(row=2,column=1,columnspan=1,sticky=W)
        self.Confirm_bttn=Button(self,text='Confirm',fg='blue',command=self.login_confirm)
        self.Confirm_bttn.grid(row=3,column=1,columnspan=1,sticky=W)
        self.Back_bttn=Button(self,text='Back',fg='black',command=self.login_back)
        self.Back_bttn.grid(row=3,column=0,columnspan=1,sticky=W)
        self.CurrentError_lbl=Label(self,text=self.CurrentError,fg='red')
        self.CurrentError_lbl.grid(row=4,column=0,columnspan=1,sticky=W)
    def login_back(self):
        self.LI_title_lbl.destroy()
        self.user_lbl.destroy()
        self.userE.destroy()
        self.pass_lbl.destroy()
        self.passE.destroy()
        self.Confirm_bttn.destroy()
        self.Back_bttn.destroy()
        self.CurrentError_lbl.destroy()
        self.initial()
    def login_confirm(self):
        self.user_E.set(self.userE.get())
        self.pass_E.set(self.passE.get())
        if self.user_E in self.userlist:
            if self.pass_E in self.passlist:
                if self.passlist.index(self.pass_E)==self.userlist.index(self.user_E):
                    self.logged_in()
                else:
                    self.CurrentError='The Password Entered \nis Incorrect'
                    self.Login_Error()
            else:
                self.CurrentError='The Password Entered \nis Incorrect'
                self.Login_Error()
        else:
            self.CurrentError='Username does not exist'
            self.Login_Error()
    def Login_Error(self):
        self.LI_title_lbl.destroy()
        self.user_lbl.destroy()
        self.userE.destroy()
        self.pass_lbl.destroy()
        self.passE.destroy()
        self.Confirm_bttn.destroy()
        self.Back_bttn.destroy()
        self.CurrentError_lbl.destroy()
        self.login()

答案 2 :(得分:1)

请参阅NSWindow.styleMask媒体资源和Window Style Masks

清除NSClosableWindowMask,NSMiniaturizableWindowMask和NSResizableWindowMask标志将从窗口的标题栏中删除所有按钮。

window.styleMask &= ~(NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask)

答案 3 :(得分:1)

我发现以下内容有效地禁用了,而没有完全隐藏关闭按钮:

self.view.window?.standardWindowButton(NSWindow.ButtonType.closeButton)?.isEnabled = false

答案 4 :(得分:1)

旁注:请勿禁用“关闭”红色按钮:苹果会拒绝应用说:

“您的应用程序的用户界面与macOS人机界面指南不一致。

具体来说,红灯被禁用了。”

:(

答案 5 :(得分:0)

如果您使用的是Storyboard,可以在IBAction或viewDidLoad中更改窗口styleMask属性,如下所示:

NSApplication.sharedApplication().windows.first?.styleMask = NSTitledWindowMask   // | NSClosableWindowMask |  NSMiniaturizableWindowMask  | NSResizableWindowMask

如果您想再次启用它们,只需取消注释样式蒙版的其余部分:

NSApplication.sharedApplication().windows.first?.styleMask = NSTitledWindowMask | NSClosableWindowMask |  NSMiniaturizableWindowMask  | NSResizableWindowMask

答案 6 :(得分:0)

通过@JohnElemans在上面的accepted answer上进行扩展,当我在macOS 10.15上使用情节提要segue进行模态演示时,这对我有用:

// On NSViewController.viewDidAppear()

if let window = self.view.window {
    window.standardWindowButton(NSWindow.ButtonType.closeButton)?.isHidden = true
    window.standardWindowButton(NSWindow.ButtonType.miniaturizeButton)?.isHidden = true
    window.standardWindowButton(NSWindow.ButtonType.zoomButton)?.isHidden = true
}

我必须添加第三行(NSWindow.ButtonType.zoomButton)才能摆脱绿色(“全屏”)按钮。