IOS Presented view controller in popup

So, I have my main controller. This view controller has a panel button element with a segue schedule with the type specified as Present As Popover.

Everything works as expected. But when you click another button in this popover view controller, it displays the full view screen. I want it to appear inside a popover. Like push / display over existing popover borders.

How can i achieve this?

I only need this behavior on the iPad. But I think this does the default.

Edit

I would prefer to do all this in a storyboard, but I also want to do this with Swift code, if necessary.

+4
source share
2 answers

This is for storyboard only.

1) Create a UIViewController (blue) and ctrl + drag (mouse) from your UIBarButtonItem to the UIViewController and select "present as Popover" (like you).

2) Click on the UIViewController (blue) and click on Editor-> embed in the navigation controller (this will be a trick that allows the remaining controller to remain in the pop-up window)

3) Create a second UIViewController (green)

4) Create a UIButton in the first UIViewController (blue) and ctrl + drag the button onto the second UIViewController (green) and select "show"

In the end, it should look like this in the Storyboard:

enter image description here

And the result:

enter image description here

enter image description here

If you want to use PopOver without a navigationBar, you can use it in a blue controller:

self.navigationController?.isNavigationBarHidden = true

, :

@IBAction func backToBlueController(sender: UIButton) {
        self.navigationController?.popViewController(animated: true)
}

:

popUp, segue barButtonItem navigationController

-

.

enter image description here

UINavigationController , navigationBar, , .

UINavigationController

+5

IBAction UIBarButtonItem :

- (IBAction)popupAction:(UIBarButtonItem *)sender {
    UIViewController *vc = [[UIViewController alloc] init];  // I don't use segue
    vc.view.backgroundColor = [UIColor grayColor];
    vc.modalPresentationStyle = UIModalPresentationPopover;
    UIPopoverPresentationController *popvc = vc.popoverPresentationController;
    popvc.delegate = self;
    popvc.permittedArrowDirections = UIPopoverArrowDirectionAny;
    popvc.barButtonItem = sender; // if UIBarButtonItem
    // if view
    // popvc.sourceView = sender;
    // popvc.sourceRect = sender.bounds;

    vc.preferredContentSize = CGSizeMake(200, 200);

    [self presentViewController:vc animated:YES completion:nil];
}

UIPopoverPresentationControllerDelegate:

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection{
    return UIModalPresentationNone;
}

- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
    return YES;
}

Swift:

@IBAction func popupAction(_ sender: UIBarButtonItem) {
    let vc = UIViewController.init()
    vc.view.backgroundColor = UIColor.gray
    vc.modalPresentationStyle = UIModalPresentationStyle.popover
    let popvc = vc.popoverPresentationController
    popvc?.delegate = self
    popvc?.permittedArrowDirections = UIPopoverArrowDirection.any
    popvc?.barButtonItem = sender
    vc.preferredContentSize = CGSize.init(width: 200, height: 200)

    self.present(vc, animated: true, completion: nil)
}

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.none
}

func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
    return true
}

iPad, iPhone.

:

afzTr.png

viewController popover:

UI9xb.png

+5

Source: https://habr.com/ru/post/1677133/


All Articles