How to make UIPopoverController keep same position after rotating?

前端 未结 13 1328
粉色の甜心
粉色の甜心 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: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;
    }
    

提交回复
热议问题