Non-resizable window swift

后端 未结 5 444
无人及你
无人及你 2020-12-09 18:48

I have a NSViewController named Hardness, and I need not to let user resize it. Of course, I can just resize it back every time the users tries, bu

相关标签:
5条回答
  • 2020-12-09 19:17

    edit/update: Xcode 10.2 • Swift 5

    NSWindow has a property called styleMask that allows you to control what kinds of control will be available to the user. If you don't want to allow the user to resize the window you have to remove the style mask .resizable using the mutating method remove(member: NSWindowStyleMask). To enable it again you need to use the mutating method insert(member: NSWindowStyleMask). Note that it will also disable the full screen mode for that window:


    removing to disable:

    window.styleMask.remove(.resizable)
    

    inserting to enable

    window.styleMask.insert(.resizable)
    

    Sample

    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

    0 讨论(0)
  • 2020-12-09 19:17

    The correct approach would be to use bitwise operators.

    Disable resize:

    window?.styleMask &= ~NSResizableWindowMask
    

    Enable resize:

    window?.styleMask |= NSResizableWindowMask
    
    0 讨论(0)
  • 2020-12-09 19:19

    I solved the same issue with the non-resizable window by one line of code in

    override func viewDidAppear() {
        self.view.window?.styleMask.remove(NSWindowStyleMask.Resizable)
    }
    
    0 讨论(0)
  • 2020-12-09 19:30

    A little more elegant solution for Swift 3, so that the | operator can be used:

    public func | (left: NSWindowStyleMask, right: NSWindowStyleMask) -> NSWindowStyleMask {
        return NSWindowStyleMask(rawValue: left.rawValue | right.rawValue)
    }
    
    0 讨论(0)
  • 2020-12-09 19:42

    This answer may be of some help in addition to the current one. There's also a nice simple way to accomplish this by using setHidden with NSWindowZoomButton

    Setup the functionality as a sub-class of NSWindow:

    Objective-C

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

    Swift

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

    Connect the custom class to your window in IB and the Zoom button should be now hidden!

    0 讨论(0)
提交回复
热议问题