Skip to content

Instantly share code, notes, and snippets.

@ravanar-sk
Forked from susanstevens/iphone-popover.md
Created November 16, 2021 06:39
Show Gist options
  • Select an option

  • Save ravanar-sk/14abd753ac95e833a8b83917eb9ef02d to your computer and use it in GitHub Desktop.

Select an option

Save ravanar-sk/14abd753ac95e833a8b83917eb9ef02d to your computer and use it in GitHub Desktop.

Revisions

  1. @susanstevens susanstevens revised this gist Feb 11, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion iphone-popover.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ### How to Create a Popover on iPhone (iOS 13)
    ### How to Create a Popover on iPhone

    #### Storyboard set up

  2. @susanstevens susanstevens revised this gist Feb 11, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions iphone-popover.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    ### How to Create a Popover on iPhone (iOS 13)

    #### Storyboard set up

    Add your view controllers in storyboard. In the Size Inspector, change the simulated size of the popover view controller to "Freeform". This doesn't change the popover's size when you run the app, but it does make it easier to lay out subviews correctly.
  3. @susanstevens susanstevens revised this gist Feb 11, 2020. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions iphone-popover.md
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,8 @@ Add your view controllers in storyboard. In the Size Inspector, change the simul

    When adding the segue between view controllers, select "Present as Popover".

    <img width="600" alt="segue" src="https://user-images.githubusercontent.com/10423328/74289839-0409c080-4d28-11ea-80a3-e5411bef139a.png">

    #### Code

    In your view controller, override the `prepare(for:sender:)` method so that you can access the popover view controller just before it appears on screen.
    @@ -47,3 +49,6 @@ extension ViewController: UIPopoverPresentationControllerDelegate {
    1. Access the popover's `popoverPresentationController`. This is the object that manages the presentation of the popover.
    2. Set your view controller as the popover presentation controller's delegate.
    3. Implement the `adaptivePresentationStyle` method to tell the popover presentation controller how to adapt to different size classes. In this case, you don't want the popover's presentation to change, so you return `.none`.

    ![popover](https://user-images.githubusercontent.com/10423328/74289829-f81dfe80-4d27-11ea-8341-5e9355ee8c86.png)

  4. @susanstevens susanstevens revised this gist Feb 11, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions iphone-popover.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@

    Add your view controllers in storyboard. In the Size Inspector, change the simulated size of the popover view controller to "Freeform". This doesn't change the popover's size when you run the app, but it does make it easier to lay out subviews correctly.

    <img width="600" alt="storyboard" src="https://user-images.githubusercontent.com/10423328/74289665-952c6780-4d27-11ea-8769-356853568d85.png">

    When adding the segue between view controllers, select "Present as Popover".

    #### Code
  5. @susanstevens susanstevens created this gist Feb 11, 2020.
    47 changes: 47 additions & 0 deletions iphone-popover.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #### Storyboard set up

    Add your view controllers in storyboard. In the Size Inspector, change the simulated size of the popover view controller to "Freeform". This doesn't change the popover's size when you run the app, but it does make it easier to lay out subviews correctly.

    When adding the segue between view controllers, select "Present as Popover".

    #### Code

    In your view controller, override the `prepare(for:sender:)` method so that you can access the popover view controller just before it appears on screen.

    Start by setting the `preferredContentSize`.

    ```swift
    class ViewController: UIViewController {

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    segue.destination.preferredContentSize = CGSize(width: 300, height: 200)
    }
    }
    ```

    At this point, if you run your app on an iPhone simulator, you'll notice that the popover view controller still covers most of the screen. By default, popovers display as full-screen when the horizontal size class is compact.

    To fix this, add the following code:

    ```swift
    class ViewController: UIViewController {

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    segue.destination.preferredContentSize = CGSize(width: 300, height: 200)
    if let presentationController = segue.destination.popoverPresentationController { // 1
    presentationController.delegate = self // 2
    }
    }
    }

    extension ViewController: UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyle(for controller: UIPresentationController,
    traitCollection: UITraitCollection) -> UIModalPresentationStyle {
    return .none // 3
    }
    }
    ```

    1. Access the popover's `popoverPresentationController`. This is the object that manages the presentation of the popover.
    2. Set your view controller as the popover presentation controller's delegate.
    3. Implement the `adaptivePresentationStyle` method to tell the popover presentation controller how to adapt to different size classes. In this case, you don't want the popover's presentation to change, so you return `.none`.