My goal is to show a View Controller programmatically
Current View Controller
and then, if some event gets called or something, (API, or Websocket) I want to call these Views programmatically
But I want to call the last view Controller first and it is supposed to be on top of the first View controller
So technically the last View will have
Transition is Cross Dissolve Presentation is Over Current Context
How would I do this?
4 Answers
Answers 1
As per your requirement, you can set a storyBoardID for your navigation controller.
On a particular event just instantiate the Navigation controller let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) let myNavController = storyboard.instantiateViewController(withIdentifier: "MyStoryboardId") as? UINavigationController
then present or show this navigation controller
self.present(myNavController, animated: true, completion: nil)
On viewDidLoad()
method of first view controller perform the segue to popupViewController.
Now the secondView will show above the first view controller. you can dismiss this view after using it.
Answers 2
You don't need to create the whole UIViewController
for popups.
You can show a usual UIView
. For example:
Create view programmatically:
let popupView: UIView = { let view = UIView() view.backgroundColor = .red view.translatesAutoresizingMaskIntoConstraints = false return view }
Insert 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 }
Also you can add views into your 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 .... }
Answers 3
I think the best way is created by xib file for reusable view. Then you can create a singleton and call this view every time when you want to show this modal view.
To ensure that your view does not overlap with anything, you must add it not to the current controller, but to AppDelegate window.
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.window!.addSubview(yourModalView)
Answers 4
You can do this by overriding your UIViewController
subclass' init methods, 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's Nib Name" in the nibName to prevent crashes from iOS 8 devices super.init(nibName: "SomeViewController's Nib Name", bundle: nil) } }
Then on your other UIViewController
s you can initialize the said ViewController.
xib and/or programatically
// 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's Identifier") as? SomeViewController { self.present(viewController, animated: true, completion: nil) }
Make sure your view hierarchy looks like this
This should be their properties
View
- backgroundColor = .clear
Translucent Dark Background View
- backgroundColor = .black
- alpha = 0.4
Popover View
.. whatever you want this to be
0 comments:
Post a Comment