Search code examples
swiftmacoscocoapositionpopover

change popover hight and unhide a new area


Good morning,

I have this litte example (swift 4 for macOS):

Storyboard

enter image description here

Result

enter image description here

In my popover are three buttons. I have an IBAction on the grey button.

Now I would like to realize the following situation, if I press the grey button:

enter image description here

  • I would like to change the height of my popover
  • the textfields under the grey button should be hide.
  • the two white buttons should move up

To change the height of my popover I tried this with success:

@IBAction func buttonPressed(_ sender: NSButton) {

   self.view.window?.animator().setFrame(
   NSRect(origin: CGPoint(x: self.view.window!.frame.origin.x, y: self.view.window!.frame.origin.y), size: CGSize(width: self.view.window!.frame.width, height: self.view.window!.frame.height - 100)), display: true, animate: true)

}

I know, that I can hide the text fields like this: mytextfield.isHidden = true but how can i move up the two white buttons on the same position, where the textfields were shown before?


Solution

  • Set the position of the buttons at where textField is.

    @IBAction func buttonPressed(_ sender: NSButton) {
        textField.isHidden = true
        textField2.isHidden = true
        buttonA.frame.origin.y = (textField.frame.origin.y + textField2.frame.origin.y)/2
        buttonB.frame.origin.y = (textField.frame.origin.y + textField2.frame.origin.y)/2
    }