How can I fix the “UIPopoverController is deprecated” warning?

后端 未结 4 644
一向
一向 2020-12-01 01:19

I am using this code:

mediaLibraryPopover = [[UIPopoverController alloc] 
                        initWithContentViewController:avc];
[self.mediaLibraryPopov         


        
相关标签:
4条回答
  • 2020-12-01 01:47

    You no longer need UIPopoverController for presenting a view controller. Instead you can set the modalPresentationStyle of view controller to UIModalPresentationPopover.

    You can use the following code for that:

    avc.modalPresentationStyle = UIModalPresentationPopover;
    avc.popoverPresentationController.sourceView = theButton;
    [self presentViewController:avc animated:YES completion:nil];
    

    UIModalPresentationPopover

    In a horizontally regular environment, a presentation style where the content is displayed in a popover view. The background content is dimmed and taps outside the popover cause the popover to be dismissed. If you do not want taps to dismiss the popover, you can assign one or more views to the passthroughViews property of the associated UIPopoverPresentationController object, which you can get from the popoverPresentationController property.

    In a horizontally compact environment, this option behaves the same as UIModalPresentationFullScreen.

    Available in iOS 8.0 and later.

    Reference UIModalPresentationStyle Reference


    You need to set either sourceView or barButtonItem property, else it will crash with the following message:

    *** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (***) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'

    For anchoring the popover arrow correctly, you need to specify the sourceRect property also.

    avc.modalPresentationStyle                   = UIModalPresentationPopover;
    avc.popoverPresentationController.sourceView = self.view;
    avc.popoverPresentationController.sourceRect = theButton.frame;
    [self presentViewController:avc animated:YES completion:nil];
    

    Refer sourceView and sourceRect for more details.

    0 讨论(0)
  • 2020-12-01 02:01

    EDIT: Regarding the down-votes. First a fun and relevant story:

    http://www.folklore.org/StoryView.py?story=Negative_2000_Lines_Of_Code.txt

    I posted the answer below in order to help coders that might have been stuck in the mindset that iPad/iPhone still needed separate execution paths when presenting a media picker UI. At the time of my original post this was not clear in the other answers. This solution path simplified my code and the future maintenance of my App. I think others might find this perspective useful because sometimes removing the right lines of code can be a good solution.

    End of edit.

    If you already have a working program and just want to get rid of the depreciation warning this could work for you.

    In my code, and in my understanding, Popovers were introduced for the iPad and are iPad specific. Apple seems to have changed that. So, if you already have a working program that uses something like this:

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        // use popovers 
    else
        //  don't use popovers
    

    what you can do is just get rid of the iPad specific code (which is probably the code using Popovers) and have your program run the same instructions for both iPhone and iPad. This worked for me.

    0 讨论(0)
  • 2020-12-01 02:06

    Apple has the official way to present and configure popovers for iOS8 here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPopoverPresentationController_class/index.html

    While similar to @MidhunMP's answer, it's worth noting the paragraph:

    Configuring the popover presentation controller after calling presentViewController:animated:completion: might seem counter-intuitive but UIKit does not create a presentation controller until after you initiate a presentation. In addition, UIKit must wait until the next update cycle to display new content onscreen anyway. That delay gives you time to configure the presentation controller for your popover.

    Configuration and responding to events can also be done via a delegate if you wanted (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPopoverPresentationControllerDelegate_protocol/index.html).

    An example, setting aside the use of the delegate:

    // Present the controller using the popover style.
    controller.modalPresentationStyle = UIModalPresentationPopover;
    [self presentViewController:controller animated:YES completion:nil];
    
    // Popover presentation controller was created when presenting; now  configure it.
    UIPopoverPresentationController *presentationController =
            [controller popoverPresentationController];
    presentationController.permittedArrowDirections = UIPopoverArrowDirectionLeft;
    presentationController.sourceView = containerFrameOfReferenceView;
    // arrow points out of the rect specified here
    presentationController.sourceRect = childOfContainerView.frame;
    

    But you'll also want to dismiss this. To do so without using a delegate, your presenting controller can just call:

    [self dismissViewControllerAnimated:YES completion:nil];
    

    But what if I rotate my device, and the popover doesn't point to the right area? Your presenting controller can handle it:

    // Respond to rotations or split screen changes
    - (void)viewWillTransitionToSize:(CGSize)size
           withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
        [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
        [coordinator animateAlongsideTransition:nil 
                                     completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
            // Fix up popover placement if necessary, after the transition.
            if (self.presentedViewController) {
                UIPopoverPresentationController *presentationController =
                        [self.presentedViewController popoverPresentationController];
                presentationController.sourceView = containerFrameOfReferenceView;
                presentationController.sourceRect = childOfContainerView.frame;
            }
        }];
    }
    
    0 讨论(0)
  • 2020-12-01 02:08

    If wanted directly from Button Action then this code can be used

    SecondViewController *destinationViewController = (SecondViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"second"];
    destinationViewController.modalPresentationStyle = UIModalPresentationPopover;
    destinationViewController.popoverPresentationController.sourceView = self.customButton;
    
    // Set the correct sourceRect given the sender's bounds
    destinationViewController.popoverPresentationController.sourceRect = ((UIView *)sender).bounds;
    [self presentViewController:destinationViewController animated:YES completion:nil];
    
    0 讨论(0)
提交回复
热议问题