How to make UIPopoverController keep same position after rotating?

前端 未结 13 1121
粉色の甜心
粉色の甜心 2021-01-30 18:21

I can\'t keep popover the same position on the screen after rotation. Is there any good way to do that, because just setting some frame to popover works terrible after rotating.

相关标签:
13条回答
  • 2021-01-30 18:33

    You can do this in didRotateFromInterfaceOrientation: method of the view controller that you used to present the popover.

    Use setPopoverContentSize:animated: method for setting the size of the popover.

    0 讨论(0)
  • 2021-01-30 18:35

    As of iOS 8.0.2 willRotateToInterfaceOrientation will not have any effect. As mhrrt mentioned, you need to use the delegate method:

    - (void)popoverController:(UIPopoverController *)popoverController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing *)view

    So for example if you want your popover to appear directly below a button that was pressed, you would use the following code:

    - (void)popoverController:(UIPopoverController *)popoverController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing *)view
    {
       CGRect rectInView = [self.theButton convertRect:self.theButton.frame toView:self.view];
       *rect = CGRectMake(CGRectGetMidX(rectInView), CGRectGetMaxY(rectInView), 1, 1);
       *view = self.view;
    }
    
    0 讨论(0)
  • 2021-01-30 18:37

    I have similar problem which I resolve by this

    [myPop presentPopoverFromRect:myfield.frame inView:myscrollview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    

    Where myfield is frame from which you want to show your popover and myscrollview is container view in which you add your popover as subview(in my case its my scrollview, instead of putting inView:self.view I use inView:myscrollview).

    0 讨论(0)
  • 2021-01-30 18:38

    I had a same problem. Instead of performing -presentPopoverFromRect each time by keeping track of the source rectangle / view from which it is presented, I subclassed UIPopoverController. After doing it, all you have to do is set either the UIBarButtonItem / UIView from where the popover has to be displayed. You can even opt for displaying the popover from custom frame which can be passed in as a NSString value.

    CSPopoverController.h:

    #import <UIKit/UIKit.h>
    
    // The original popover controller would not re-orientate itself when the orientation change occurs. To tackle that issue, this subclass is created
    @interface CSPopoverController : UIPopoverController
    
    @property (nonatomic, strong) NSString *popoverDisplaySourceFrame;  // Mutually Exclusive. If you want to set custom rect as source, make sure that popOverDisplaySource is nil
    @property (nonatomic, strong) id popoverDisplaySource;              // Mutually exclusive. If UIBarButtonItem is set to it, popoverDisplaySourceFrame is neglected.
    @property (nonatomic, strong) UIView *popoverDisplayView;
    
    @property (nonatomic, assign, getter = shouldAutomaticallyReorientate) BOOL automaticallyReorientate;
    
    -(void)reorientatePopover;
    
    @end
    

    CSPopoverController.m:

    #import "CSPopoverController.h"
    
    @implementation CSPopoverController
    @synthesize popoverDisplaySourceFrame = popoverDisplaySourceFrame_;
    -(NSString*)popoverDisplaySourceFrame
    {
        if (nil==popoverDisplaySourceFrame_)
        {
            if (nil!=self.popoverDisplaySource)
            {
                if ([self.popoverDisplaySource isKindOfClass:[UIView class]])
                {
                    UIView *viewSource = (UIView*)self.popoverDisplaySource;
                    [self setPopoverDisplaySourceFrame:NSStringFromCGRect(viewSource.frame)];
                }
            }
        }
        return popoverDisplaySourceFrame_;
    }
    -(void)setPopoverDisplaySourceFrame:(NSString *)inPopoverDisplaySourceFrame
    {
        if (inPopoverDisplaySourceFrame!=popoverDisplaySourceFrame_)
        {
            popoverDisplaySourceFrame_ = inPopoverDisplaySourceFrame;
            [self reorientatePopover];
        }
    }
    @synthesize popoverDisplaySource = popoverDisplaySource_;
    -(void)setPopoverDisplaySource:(id)inPopoverDisplaySource
    {
        if (inPopoverDisplaySource!=popoverDisplaySource_)
        {
            [self unlistenForFrameChangeInView:popoverDisplaySource_];
            popoverDisplaySource_ = inPopoverDisplaySource;
            [self reorientatePopover];
    
            if ([popoverDisplaySource_ isKindOfClass:[UIView class]])
            {
                UIView *viewSource = (UIView*)popoverDisplaySource_;
                [self setPopoverDisplaySourceFrame:NSStringFromCGRect(viewSource.frame)];
            }
            if (self.shouldAutomaticallyReorientate)
            {
                [self listenForFrameChangeInView:popoverDisplaySource_];
            }
        }
    }
    @synthesize popoverDisplayView = popoverDisplayView_;
    -(void)setPopoverDisplayView:(UIView *)inPopoverDisplayView
    {
        if (inPopoverDisplayView!=popoverDisplayView_)
        {
            popoverDisplayView_ = inPopoverDisplayView;
            [self reorientatePopover];
        }
    }
    @synthesize automaticallyReorientate = automaticallyReorientate_;
    -(void)setAutomaticallyReorientate:(BOOL)inAutomaticallyReorientate
    {
        if (inAutomaticallyReorientate!=automaticallyReorientate_)
        {
            automaticallyReorientate_ = inAutomaticallyReorientate;
            if (automaticallyReorientate_)
            {
                [self listenForAutorotation];
                [self listenForFrameChangeInView:self.popoverDisplaySource];
            }
            else
            {
                [self unlistenForAutorotation];
                [self unlistenForFrameChangeInView:self.popoverDisplaySource];
            }
        }
    }
    
    -(void)listenForAutorotation
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(orientationChanged:)
                                                     name:UIDeviceOrientationDidChangeNotification
                                                   object:nil];
    }
    
    -(void)unlistenForAutorotation
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:UIDeviceOrientationDidChangeNotification
                                                      object:nil];
    }
    
    -(void)listenForFrameChangeInView:(id)inView
    {
        // Let's listen for changes in the view's frame and adjust the popover even if the frame is updated
        if ([inView isKindOfClass:[UIView class]])
        {
            UIView *viewToObserve = (UIView*)inView;
            [viewToObserve addObserver:self
                            forKeyPath:@"frame"
                               options:NSKeyValueObservingOptionNew
                               context:nil];
        }
    }
    
    -(void)unlistenForFrameChangeInView:(id)inView
    {
        if ([inView isKindOfClass:[UIView class]])
        {
            UIView *viewToObserve = (UIView*)inView;
            [viewToObserve removeObserver:self
                               forKeyPath:@"frame"];
        }
    }
    
    // TODO: Dealloc is not called, check why? !!!
    - (void)dealloc
    {
        [self unlistenForFrameChangeInView:self.popoverDisplaySource];
        [self unlistenForAutorotation];
        DEBUGLog(@"dealloc called for CSPopoverController %@", self);
    }
    
    #pragma mark - Designated initializers
    -(id)initWithContentViewController:(UIViewController *)viewController
    {
        self = [super initWithContentViewController:viewController];
        if (self)
        {
            [self popoverCommonInitializations];
        }
        return self;
    }
    
    -(void)popoverCommonInitializations
    {
        [self setAutomaticallyReorientate:YES];
    }
    
    #pragma mark - Frame
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if (object==self.popoverDisplaySource)
        {
            [self setPopoverDisplaySourceFrame:nil];
            [self reorientatePopover];
        }
    }
    
    #pragma mark - Orientation
    -(void)orientationChanged:(NSNotification *)inNotification
    {
        [self reorientatePopover];
    }
    
    -(void)reorientatePopover
    {
        [NSObject cancelPreviousPerformRequestsWithTarget:self
                                                 selector:@selector(performReorientatePopover)
                                                   object:nil];
    //    if ([self isPopoverVisible])
        {
            [self performSelector:@selector(performReorientatePopover)
                       withObject:nil
                       afterDelay:0.0];
        }
    }
    
    -(void)performReorientatePopover
    {
        if (self.popoverDisplaySourceFrame && self.popoverDisplayView)
        {
            [self presentPopoverFromRect:CGRectFromString(self.popoverDisplaySourceFrame)
                                  inView:self.popoverDisplayView
                permittedArrowDirections:UIPopoverArrowDirectionAny
                                animated:YES];
        }
        else if (self.popoverDisplaySource && [self.popoverDisplaySource isKindOfClass:[UIBarButtonItem class]])
        {
            UIBarButtonItem *barButton = (UIBarButtonItem*)self.popoverDisplaySource;
            [self presentPopoverFromBarButtonItem:barButton
                         permittedArrowDirections:UIPopoverArrowDirectionAny
                                         animated:YES];
        }
    }
    
    @end
    

    Usage:

    If it is a UIBarButtonItem from where you are presenting it:

    CSPopoverController *popOverCont = [[CSPopoverController alloc]initWithContentViewController:navCont];
    self.popOver = popOverCont;
    [popOverCont setPopoverDisplaySource:self.settingsButtonItem];
    

    If it is a UIView from where you are presenting the popover:

    CSPopoverController *popOver = [[CSPopoverController alloc] initWithContentViewController:navigation];
    self.iPadPopoverController = popOver;
    [newDateVC setIPadPopoverController:self.iPadPopoverController];
    [popOver setPopoverDisplaySource:inButton];
    [popOver setPopoverDisplayView:inView];
    
    0 讨论(0)
  • 2021-01-30 18:38

    For iOS > 8 John Strickers answer helped but didn't do what I wanted it to do.

    Here's the solution that worked for me. (If you want to download a full sample project it's here: https://github.com/appteur/uipopoverExample)

    I created a property to hold any popover I wanted to present and also added a property to track the sourceRect and another for the view of the button I wanted the popover arrow to point at.

    @property (nonatomic, weak) UIView *activePopoverBtn;
    @property (nonatomic, strong) PopoverViewController *popoverVC;
    @property (nonatomic, assign) CGRect sourceRect; 
    

    The button that triggered my popover is in a UIToolbar. When tapped it runs the following method that creates and launches the popover.

    -(void) buttonAction:(id)sender event:(UIEvent*)event
    {
        NSLog(@"ButtonAction");
    
        // when the button is tapped we want to display a popover, so setup all the variables needed and present it here
    
        // get a reference to which button's view was tapped (this is to get 
        // the frame to update the arrow to later on rotation)
        // since UIBarButtonItems don't have a 'frame' property I found this way is easy
        UIView *buttonView          = [[event.allTouches anyObject] view];
    
        // set our tracker properties for when the orientation changes (handled in the viewWillTransitionToSize method above)
        self.activePopoverBtn       = buttonView;
        self.sourceRect             = buttonView.frame;
    
        // get our size, make it adapt based on our view bounds
        CGSize viewSize             = self.view.bounds.size;
        CGSize contentSize          = CGSizeMake(viewSize.width, viewSize.height - 100.0);
    
        // set our popover view controller property
        self.popoverVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"PopoverVC"];
    
        // configure using a convenience method (if you have multiple popovers this makes it faster with less code)
        [self setupPopover:self.popoverVC
            withSourceView:buttonView.superview // this will be the toolbar
                sourceRect:self.sourceRect
               contentSize:contentSize];
    
        [self presentViewController:self.popoverVC animated:YES completion:nil];
    
    }
    

    The 'setupPopover:withSourceView:sourceRect:contentSize method is simply a convenience method to set the popoverPresentationController properties if you plan to display multiple popovers and want them configured the same. It's implementation is below.

    // convenience method in case you want to display multiple popovers
    -(void) setupPopover:(UIViewController*)popover withSourceView:(UIView*)sourceView sourceRect:(CGRect)sourceRect contentSize:(CGSize)contentSize
    {
        NSLog(@"\npopoverPresentationController: %@\n", popover.popoverPresentationController);
    
        popover.modalPresentationStyle = UIModalPresentationPopover;
        popover.popoverPresentationController.delegate = self;
        popover.popoverPresentationController.sourceView                = sourceView;
        popover.popoverPresentationController.sourceRect                = sourceRect;
        popover.preferredContentSize                                    = contentSize;
        popover.popoverPresentationController.permittedArrowDirections  = UIPopoverArrowDirectionDown;
        popover.popoverPresentationController.backgroundColor           = [UIColor whiteColor];
    }
    

    For iOS 8 and up the viewWillTransitionToSize:withTransitionCoordinator get's called on the view controller when the device rotates.

    I implemented this method in my presenting view controller class as shown below.

    // called when rotating a device
    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
    {
        NSLog(@"viewWillTransitionToSize [%@]", NSStringFromCGSize(size));
    
        // resizes popover to new size and arrow location on orientation change
        [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context)
        {
            if (self.popoverVC)
            {
                // get the new frame of our button (this is our new source rect)
                CGRect viewframe = self.activePopoverBtn ? self.activePopoverBtn.frame : CGRectZero;
    
                // update our popover view controller's sourceRect so the arrow will be pointed in the right place
                self.popoverVC.popoverPresentationController.sourceRect = viewframe;
    
                // update the preferred content size if we want to adapt the size of the popover to fit the new bounds
                self.popoverVC.preferredContentSize = CGSizeMake(self.view.bounds.size.width -20, self.view.bounds.size.height - 100);
            }
    
        } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
            // anything you want to do when the transition completes
        }];
    }
    
    0 讨论(0)
  • 2021-01-30 18:44

    In iOS 7 you can use - (void)popoverController:(UIPopoverController *)popoverController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing *)view to reposition your UIPopoverController's view on interface orientation change.

    See the UIPopoverControllerDelegate documentation.

    0 讨论(0)
提交回复
热议问题