ios - How to present a ViewController modally from the presentingViewController?

Ios - How to present a ViewController modally from the presentingViewController?

Presenting a UIViewController modally in iOS is a common task. You can achieve this by calling the present(_:animated:completion:) method on the presentingViewController. Here's a step-by-step guide on how to do this:

Step 1: Create the View Controller to be Presented

First, ensure that you have the view controller you want to present. This could be defined in your storyboard, in a XIB file, or programmatically.

Example: Creating a View Controller Programmatically

import UIKit class ModalViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white // Add any additional setup for this view controller } } 

Step 2: Present the View Controller Modally

To present the ModalViewController modally from another view controller (let's call it presentingViewController), you would use the present(_:animated:completion:) method.

Example: Presenting Modally from Another View Controller

import UIKit class PresentingViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white // Create a button to present the modal view controller let button = UIButton(type: .system) button.setTitle("Present Modally", for: .normal) button.addTarget(self, action: #selector(presentModalViewController), for: .touchUpInside) button.frame = CGRect(x: 100, y: 100, width: 200, height: 50) view.addSubview(button) } @objc func presentModalViewController() { let modalVC = ModalViewController() modalVC.modalPresentationStyle = .fullScreen // Or .pageSheet, .formSheet, etc. present(modalVC, animated: true, completion: nil) } } 

Explanation

  1. Define the Modal View Controller:

    class ModalViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white } } 
  2. Presenting the Modal View Controller:

    • Create an instance of ModalViewController.
    • Set the modal presentation style (optional, defaults to .automatic).
    • Call the present method on the presenting view controller.
    @objc func presentModalViewController() { let modalVC = ModalViewController() modalVC.modalPresentationStyle = .fullScreen // Or any other modal presentation style present(modalVC, animated: true, completion: nil) } 

Modal Presentation Styles

The modalPresentationStyle property of UIViewController allows you to specify how the modal view controller is presented. Here are some common styles:

  • .automatic: Default presentation style chosen by the system.
  • .fullScreen: Presents the view controller full screen, covering the presenting view controller.
  • .pageSheet: Presents the view controller as a page sheet on iPad or full screen on iPhone.
  • .formSheet: Presents the view controller as a form sheet on iPad or full screen on iPhone.
  • .currentContext: Uses the same presentation context as its presenting view controller.
  • .custom: Uses a custom presentation controller.

Example with Storyboard

If you are using a storyboard, you can give your view controller a storyboard ID and instantiate it using UIStoryboard.

@objc func presentModalViewController() { let storyboard = UIStoryboard(name: "Main", bundle: nil) let modalVC = storyboard.instantiateViewController(withIdentifier: "ModalViewControllerID") modalVC.modalPresentationStyle = .fullScreen present(modalVC, animated: true, completion: nil) } 

Replace "Main" with the name of your storyboard and "ModalViewControllerID" with the storyboard ID you set for the modal view controller.

By following these steps, you can present a view controller modally in your iOS app from the presenting view controller.

Examples

  1. iOS Present ViewController Modally

    let viewController = UIViewController() viewController.modalPresentationStyle = .fullScreen presentingViewController?.present(viewController, animated: true, completion: nil) 

    Description: Presents a new view controller (viewController) modally from the presenting view controller (presentingViewController) with full-screen presentation style.

  2. iOS Present ViewController Programmatically

    let viewController = UIViewController() viewController.modalPresentationStyle = .overCurrentContext presentingViewController?.present(viewController, animated: true, completion: nil) 

    Description: Programmatically presents a view controller (viewController) modally over the current context from the presenting view controller.

  3. iOS Present ViewController with Custom Animation

    let viewController = UIViewController() viewController.modalPresentationStyle = .popover viewController.modalTransitionStyle = .coverVertical presentingViewController?.present(viewController, animated: true, completion: nil) 

    Description: Presents a view controller (viewController) modally with popover presentation style and covers vertically as transition style from the presenting view controller.

  4. iOS Present ViewController with Navigation Controller

    let viewController = UIViewController() let navController = UINavigationController(rootViewController: viewController) navController.modalPresentationStyle = .formSheet presentingViewController?.present(navController, animated: true, completion: nil) 

    Description: Presents a view controller (viewController) embedded in a navigation controller (navController) modally with a form sheet presentation style from the presenting view controller.

  5. iOS Present ViewController from TabBarController

    let viewController = UIViewController() viewController.modalPresentationStyle = .pageSheet presentingViewController?.tabBarController?.present(viewController, animated: true, completion: nil) 

    Description: Presents a view controller (viewController) modally with a page sheet presentation style from the tab bar controller of the presenting view controller.

  6. iOS Present ViewController from SplitViewController

    let viewController = UIViewController() viewController.modalPresentationStyle = .automatic presentingViewController?.splitViewController?.present(viewController, animated: true, completion: nil) 

    Description: Presents a view controller (viewController) modally with an automatic presentation style from the split view controller of the presenting view controller.

  7. iOS Present ViewController with Custom Size

    let viewController = UIViewController() viewController.modalPresentationStyle = .custom viewController.preferredContentSize = CGSize(width: 300, height: 400) presentingViewController?.present(viewController, animated: true, completion: nil) 

    Description: Presents a view controller (viewController) modally with a custom presentation style and preferred size from the presenting view controller.

  8. iOS Present ViewController with Transition Animation

    let viewController = UIViewController() viewController.modalPresentationStyle = .currentContext viewController.modalTransitionStyle = .flipHorizontal presentingViewController?.present(viewController, animated: true, completion: nil) 

    Description: Presents a view controller (viewController) modally with current context presentation style and horizontal flip transition animation from the presenting view controller.

  9. iOS Present ViewController from Custom Container ViewController

    let viewController = UIViewController() viewController.modalPresentationStyle = .popover customContainerViewController.present(viewController, animated: true, completion: nil) 

    Description: Presents a view controller (viewController) modally with popover presentation style from a custom container view controller (customContainerViewController).

  10. iOS Present ViewController with Completion Handler

    let viewController = UIViewController() viewController.modalPresentationStyle = .fullScreen presentingViewController?.present(viewController, animated: true) { // Completion handler code print("Presentation completed") } 

    Description: Presents a view controller (viewController) modally with full-screen presentation style from the presenting view controller and executes a completion handler after presentation.


More Tags

nginfinitescroll microsoft-graph-files gmt http2 google-docs vue-loader apollo-server yahoo-finance ora-06512 sumoselect.js

More Programming Questions

More Fitness-Health Calculators

More Dog Calculators

More Various Measurements Units Calculators

More Retirement Calculators