0 Replies
      Latest reply on Jul 2, 2019 4:18 PM by Tomato
      Tomato Level 1 Level 1 (15 points)

        How can I can add autoresizing masks to a button that you create programmatically?

         

        import Cocoa
        
        
        class ViewController: NSViewController {
            override func viewDidAppear() {
                super.viewDidAppear()
                
                let button = NSButton(frame: CGRect(x: (view.frame.width - 80.0)/2.0, y: 320.0, width: 80.0, height: 22.0))
                button.autoresizingMask.insert(NSView.AutoresizingMask.maxYMargin)
                button.isBordered = true
                button.isTransparent = false
                button.bezelStyle = .rounded
                button.setButtonType(.momentaryChange)
                button.title = "Stop!"
                button.action = #selector(stopClicked(_:))
                view.addSubview(button)
            }
            
            @objc func stopClicked(_ sender: NSButton) {
                print("Stop me")
            }
        }
        

         

        If I run the code above, a button will be created.  And it will stick to its superview as if it had minXMargin and minYMargin masks.  So the button will stay at the bottom-left corner if you expand the window size.