How do I programmatically program pop pop view?

My goal is to programmatically view the View Controller

Current view controller

enter image description here

and then if any event is triggered or something else (API or Websocket), I want to programmatically name these views

enter image description here

But I want to call the last view controller first and it must be on top of the first View controller.

enter image description here

So technically the last view will have

Transition is Cross Dissolve Presentation is Over Current Context 

How can I do it?

+7
ios swift
source share
4 answers

I think the best way to view it multiple times is to create a XIB file. You can then create a singleton and call this view every time you want to show this modal view.

So that your view does not overlap with anything, you should add it not to the current controller, but to the AppDelegate window.

 let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.window!.addSubview(yourModalView) 
+5
source share

According to your requirement, you can set storyBoardID for your navigation controller.

At a specific event, just create an instance of the navigation controller let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) let myNavController = storyboard.instantiateViewController(withIdentifier: "MyStoryboardId") as? UINavigationController let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) let myNavController = storyboard.instantiateViewController(withIdentifier: "MyStoryboardId") as? UINavigationController

then submit or show this navigation controller

self.present(myNavController, animated: true, completion: nil)

In viewDidLoad() mode, the controller of the first view executes a segue for the popupViewController.

Now a second screen will appear above the first view controller. You can decline this submission after using it.

+4
source share

You do not need to create whole UIViewController for popups.

You can show a regular UIView . For example:

Create view programmatically:

 let popupView: UIView = { let view = UIView() view.backgroundColor = .red view.translatesAutoresizingMaskIntoConstraints = false return view } 

Insert a view when you need:

 private func showPopup() { view.addSubview(popupView) popupView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true popupView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true popupView.widthAnchor.constraint(equalToConstant: 100.0).isActive = true popupView.heightAnchor.constraint(equalToConstant: 100.0).isActive = true } 

You can also add views to popupView :

  let myLabel: UILabel = { let label = UILabel() label.text = "Test text" label.backgroundColor = .red label.translatesAutoresizingMaskIntoConstraints = false return label } private func setupPopup() { // add UILabel for example popupView.addSubview(myLabel) // Setup constraints for myLabel .... } 
+4
source share

You can do this by overriding your initialization methods of a subclass of UIViewController like this.

 class SomeViewController: UIViewController { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.modalTransitionStyle = .crossDissolve self.modalPresentationStyle = .overFullScreen } override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) self.modalTransitionStyle = .crossDissolve self.modalPresentationStyle = .overFullScreen } // if this is a xib UIViewController, if not do not add this code required init() { // place the "SomeViewController Nib Name" in the nibName to prevent crashes from iOS 8 devices super.init(nibName: "SomeViewController Nib Name", bundle: nil) } } 

Then on another UIViewController you can initialize the specified ViewController.

xib and / or programmatically

 // instantiate your UIViewController let viewController = SomeViewController() self.present(viewController, animated: true, completion: nil) 

storyboard

 // instantiate the storyboard containing your UIViewController let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil) // place UIViewController initialization inside if let block to prevent unwanted crashes if let viewController = storyboard.instantiateViewController(withIdentifier: "SomeViewController Identifier") as? SomeViewController { self.present(viewController, animated: true, completion: nil) } 

Make sure your view hierarchy looks like this:

enter image description here

These should be their properties.

View

  • backgroundColor = .clear

Transparent dark background

  • backgroundColor = .black
  • alpha = 0.4

Popover view

.. all you want it to be

+4
source share

All Articles