3

I am trying to show a view controller as UIPopoverPresentationController below the button or in center of window. But it is always showing as full window modal popup.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MySecondViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"Pop"];

    // present the controller
    // on iPad, this will be a Popover
    // on iPhone, this will be an action sheet
    controller.modalPresentationStyle = UINavigationControllerOperationPop;
    [self presentViewController:controller animated:YES completion:nil];
    controller.preferredContentSize = CGSizeMake(280, 230);
    // configure the Popover presentation controller
    UIPopoverPresentationController *popController = [controller popoverPresentationController];
    popController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    popController.delegate = self;

    // in case we don't have a bar button as reference
    popController.sourceView = self.showPop;
    popController.sourceRect = CGRectMake(384, -120, 280, 230);


-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}
Muhammad Umair
  • 583
  • 11
  • 32

4 Answers4

4

Try this code it is working

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SecondViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"pop"];

controller.modalPresentationStyle = UIModalPresentationPopover;
controller.preferredContentSize = CGSizeMake(280, 230);
// configure the Popover presentation controller

controller.popoverPresentationController.delegate = self;
controller.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;


// in case we don't have a bar button as reference
controller.popoverPresentationController.sourceView = self.view;
controller.popoverPresentationController.sourceRect = CGRectMake(384, -120, 280, 230);
//    controller.presentationController.delegate = self;
[self presentViewController:controller animated:YES completion:nil];
Hassan Aftab
  • 634
  • 2
  • 9
  • 20
  • i'm going to use bar button...it shows me tablewiew on fullscreen...what should i do...@Hassan Aftab – Suraj Sukale Jun 02 '16 at 10:17
  • 1
    @SurajSukale, it might be too late for you, but perhaps somebody else can benefit from this comment. You should assign a delegate to controller.popoverPresentationController and in the delegate implement method `adaptivePresentationStyle(for: UIPresentationController)`, returning value `UIModalPresentationStyleNone` – Lukas1 Jul 03 '17 at 06:13
2

I have posted another question for the same question and i have resolved my issue. Here is the link of question: UIPopoverPresentationController is showing full screen modal on iPhone

In ViewController.h Firstly make a property of UIPopoverPresenatationController.

@property(nonatomic,retain)UIPopoverPresentationController *dateTimePopover8;

Then to show PopOverPresentationcontroller

    UINavigationController *destNav = [[UINavigationController alloc] initWithRootViewController:dateVC];
/*Here dateVC is controller you want to show in popover*/
                dateVC.preferredContentSize = CGSizeMake(280,200);
                destNav.modalPresentationStyle = UIModalPresentationPopover;
                _dateTimePopover8 = destNav.popoverPresentationController;
                _dateTimePopover8.delegate = self;
                _dateTimePopover8.sourceView = self.view;
                _dateTimePopover8.sourceRect = [sender frame];
                destNav.modalPresentationStyle = UIModalPresentationPopover;
                destNav.navigationBarHidden = YES;
                [self presentViewController:destNav animated:YES completion:nil];

You must have noticed that we are presenting View Controller instead of presenting popOver.So we have to hide this in new way also.It hides automatically when we click on screen.

-(void)hideIOS8PopOver
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

We have to implement the delegate of UIPopoverPresenatationController in implementation file.Write below delegate method in implementation file.

- (UIModalPresentationStyle) adaptivePresentationStyleForPresentationController: (UIPresentationController * ) controller {
    return UIModalPresentationNone;
}
Community
  • 1
  • 1
Muhammad Umair
  • 583
  • 11
  • 32
0

In Storyboard this is very easy. Just control drag from the control that will trigger the action (say a UIBarButton or a normal button) to the storyboard view controller (if root view of Navigation controller, drag to the navigation controller). Select the segue and change the Kind in the attribute inspector to "Present Modally", presentation: Form sheet (if you want it to show at the centre), select the transition type you want (default is cool).

Attribute Inspector screenshot

m00am
  • 5,910
  • 11
  • 53
  • 69
Oluwatobi Omotayo
  • 1,719
  • 14
  • 28
0

As @Lukas1 mentioned above, you should conform UIPopoverPresentationControllerDelegate and implement adaptivePresentationStyle method (which is actually defined in UIAdaptivePresentationControllerDelegate protocol, UIPopoverPresentationControllerDelegate parent class) and return .none And here's implementation in Swift5 :

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

Alex L
  • 309
  • 2
  • 9