Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom pop up view with swift?

So I am working on a simple app, where I use a table view. This table view displays the name of "players". But in order for me to add players in the table view, I want a pop up window to be displayed with a text field where you provide the name.

Now I have been reading about creating a xib or nib file, but I am not sure how to "load" the pop up window.

What's the best approach to this?

Looks like this:

Pop up View

enter image description here

like image 552
J Arango Avatar asked Nov 29 '15 20:11

J Arango


2 Answers

In storyboard

  • In storyboard create one UIViewController and design it according to your requirement.
  • Set custom class as anotherViewController and Storyboard_ID as another_view_sid
  • Create one new Cocoa Touch Class as anotherViewController and subclass of UIVIewController

In viewController

    let popvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "another_view_sid") as! anotherViewController

    self.addChildViewController(popvc)

    popvc.view.frame = self.view.frame

    self.view.addSubview(popvc.view)

    popvc.didMove(toParentViewController: self)

In anotherViewController

override func viewDidLoad() {
    super.viewDidLoad()
        showAnimate()
    }
}

@IBAction func Close_popupView(_ sender: Any) {
    removeAnimate()
}

func showAnimate()
{
    self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
    self.view.alpha = 0.0
    UIView.animate(withDuration: 0.25, animations: {
        self.view.alpha = 1.0
        self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
    })
}

func removeAnimate()
{
    UIView.animate(withDuration: 0.25, animations: {
        self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
        self.view.alpha = 0.0
    }, completion: {(finished : Bool) in
        if(finished)
        {
            self.willMove(toParentViewController: nil)
            self.view.removeFromSuperview()
            self.removeFromParentViewController()
        }
    })
}
like image 197
Mili Shah Avatar answered Oct 20 '22 04:10

Mili Shah


you'd create an custom UIView with all respected object needed, from your Controller's viewDidLoad() you'll hide it.

customView.hidden = true

Whenever your user wants to perform some action or task, you unhide it and once the user finished then hide it again or remove from the superView.

customView.hidden = false

Below there is some code to help you start

    private var customView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        customView.hidden = true
    }

    private func loadCustomViewIntoController() {
         let customViewFrame = CGRect(x: 0, y: 0, witdh: view.frame.width, height: view.frame.height - 200)
         customView = UIView(frame: customViewFrame)

         view.addSubview(customView)

         customView.hidden = false

        // any other objects should be tied to this view as superView 
        // for example adding this okayButton

        let okayButtonFrame = CGRect(x: 40, y: 100, width: 50, height: 50)
        let okayButton = UIButton(frame: okayButtonFrame )

         // here we are adding the button its superView
         customView.addSubview(okayButton)

         okayButton.addTarget(self, action: #selector(self.didPressButtonFromCustomView:), forControlEvents:.TouchUpInside)

    }


    func didPressButtonFromCustomView(sender:UIButton) {
         // do whatever you want
         // make view disappears again, or remove from its superview
    }


    @IBAction func rateButton(sender:UIBarButtonItem) {
        // this barButton is located at the top of your tableview navigation bar
        // when it pressed make sure you remove any other activities that were on the screen, for example dismiss a keyboard 

          loadCustomViewIntoController()
    }

Check it out this github project, It's closed to be production ready, it gives you a better way to deal (present and dismiss) with UIViews

If you only want the player's name then use a UIAlertController containing a textfield

like image 30
Lamour Avatar answered Oct 20 '22 05:10

Lamour