【问题标题】:Non-resizable window swift不可调整大小的窗口swift
【发布时间】:2015-03-30 05:15:33
【问题描述】:

我有一个名为HardnessNSViewController,我不需要让用户调整它的大小。当然,每次用户尝试时我都可以将其大小调整回来,但是有什么办法不让用户将窗口打开到全屏或拉伸窗口?

【问题讨论】:

    标签: macos swift nswindow window-resize nsviewcontroller


    【解决方案1】:

    编辑/更新:Xcode 10.2 • Swift 5

    NSWindow 有一个名为 styleMask 的属性,它允许您控制哪些类型的控件可供用户使用。如果您不想让用户调整窗口大小,则必须使用变异方法 remove(member: NSWindowStyleMask) 删除样式掩码 .resizable。要再次启用它,您需要使用变异方法insert(member: NSWindowStyleMask)。请注意,它还会禁用该窗口的全屏模式:


    移除以禁用:

    window.styleMask.remove(.resizable)
    

    插入启用

    window.styleMask.insert(.resizable)
    

    样本

    import Cocoa
    class ViewController: NSViewController {
        @IBOutlet weak var closable: NSButton!
        @IBOutlet weak var miniaturizable: NSButton!
        @IBOutlet weak var resizable: NSButton!
        @IBOutlet weak var titled: NSButton!
        lazy var window: NSWindow! = self.view.window
        func remove(_ member: NSWindow.StyleMask) {
            window.styleMask.remove(member)
        }
        func insert(_ member: NSWindow.StyleMask) {
            window.styleMask.insert(member)
        }
        @IBAction func toggle(_ sender: NSButton) {
            switch sender.state {
            case .on:
                switch sender {
                case closable: insert(.closable)
                case miniaturizable: insert(.miniaturizable)
                case resizable: insert(.resizable)
                case closable: insert(.closable)
                case titled: insert(.titled)
                default: break
                }
            case .off:
                switch sender {
                case closable: remove(.closable)
                case miniaturizable: remove(.miniaturizable)
                case resizable: remove(.resizable)
                case closable: remove(.closable)
                case titled: remove(.titled)
                default: break
                }
            default: break
            }
        }
    }
    

    Sample Project

    【讨论】:

    • 我在 viewDidLoad 中写了这段代码:self.view.window?.styleMask = NSClosableWindowMask | NSTitledWindowMask | NSMiniaturizableWindowMask,但没有用
    • 哦,这段代码需要在 viewDidAppear 中,才行,谢谢!
    • window.styleMask = [.closable, .titled, .miniaturizable] //Swift 3
    【解决方案2】:

    我通过一行代码解决了不可调整大小窗口的相同问题

    override func viewDidAppear() {
        self.view.window?.styleMask.remove(NSWindowStyleMask.Resizable)
    }
    

    【讨论】:

    • 我更喜欢这样。
    【解决方案3】:

    正确的方法是使用bitwise operators

    禁用调整大小:

    window?.styleMask &= ~NSResizableWindowMask
    

    启用调整大小:

    window?.styleMask |= NSResizableWindowMask
    

    【讨论】:

    • 嗨。任何想法如何将其移植到 Swift 3? Binary operator '|=' cannot be applied to two 'NSWindowStyleMask' operands
    • 我不知道为什么会这样,但这里有一个解决方法:window?.styleMask = NSWindowStyleMask(rawValue: (NSWindowStyleMask.closable.rawValue | NSWindowStyleMask.titled.rawValue))
    • 它将是window?.styleMask.update(with: NSWindowStyleMask.resizable)
    【解决方案4】:

    除了当前的答案之外,此答案可能会有所帮助。还有一个很好的简单方法可以通过使用setHiddenNSWindowZoomButton 来完成此操作

    将功能设置为NSWindow的子类:

    Objective-C

    #import "CustomWindow.h"
    
    @implementation CustomWindow
    
    - (void)awakeFromNib {
    
    NSButton *zoomButton = [self standardWindowButton:NSWindowZoomButton];
    [zoomButton setHidden:YES];
    
    }
    
    @end
    

    斯威夫特

    import CustomWindow
    
    class CustomWindow {
    
        func awakeFromNib() {
            var zoomButton: NSButton = self.standardWindowButton(NSWindowZoomButton)
            zoomButton.setHidden(true)
        }
    }
    

    将自定义类连接到您在 IB 中的窗口,现在应该隐藏 Zoom 按钮!

    【讨论】:

    • 不应该吗?类 CustomWindow: NSWindow { 覆盖 func awakeFromNib() { var zoomButton: NSButton = self.standardWindowButton(NSWindowButton.ZoomButton)! zoomButton.enabled=true } } 我尝试为关闭按钮做同样的事情,但它不起作用..
    • @Do2:尝试使用self.standardWindowButton(NSWindowButton.CloseButton)?.hidden = true ...我认为自从我最初回答了这个问题以来,苹果对其进行了一些更改。
    • var closeButton: NSButton = self.standardWindowButton(NSWindowButton.CloseButton)! closeButton.hidden=true ?我还尝试从视图中调用 viewcontroller 的 viewDidAppear 的 awakefromnib 它在展开可选时由于 nil 而崩溃,有什么想法吗?
    • 您是要隐藏缩放按钮还是关闭按钮?您使用的是什么版本的 swift 和 OS X?我将发布更完整的要点,您可以稍后尝试。
    • 我使用模态视图,所以关闭按钮在第一次转场后消失,然后在 2-3 次转场后,最小化和调整大小被隐藏,但如果你将鼠标悬停在它们上面,我想要所有无论执行了多少转场,都会显示按钮。 swift 2.2 el capitan
    【解决方案5】:

    为 Swift 3 提供了一个更优雅的解决方案,这样 |可以使用运算符:

    public func | (left: NSWindowStyleMask, right: NSWindowStyleMask) -> NSWindowStyleMask {
        return NSWindowStyleMask(rawValue: left.rawValue | right.rawValue)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      • 2015-05-26
      • 2016-09-14
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多