代码之家  ›  专栏  ›  技术社区  ›  Knodel

覆盖和方向改变

  •  9
  • Knodel  · 技术社区  · 16 年前

    在我的应用程序中,我有3个UIPopover。当用户点击工具栏中的按钮时,它们就会出现。如果popover已经打开,我需要让popover在用户旋转iPad时出现在正确的位置(比如-willanimaterotationtofaceorientation:)。

    9 回复  |  直到 16 年前
        1
  •  11
  •   Nitin Gupta    12 年前

    在iOS7.0及更高版本中,可以通过实现中提供的以下方法来实现 UIPopoverControllerDelegate :

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

    PresentPooverFromRect演示 方法,当接口方向更改时,popover控制器调用此方法。

        2
  •  6
  •   Knodel    16 年前

    到目前为止,我找到的唯一解决方案就是在设备旋转时关闭popover/

        3
  •  4
  •   inwit    14 年前

    didRotateFromInterfaceOrientation: ,发送到视图控制器 之后 willRotate... 和 willAnimateRotation... 之前 旋转已经发生了,所以这是错误的地方 presentPopover... 方法调用。)

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
      // if the popover is showing, adjust its position after the re-orientation by presenting it again:
      if (self.myPopoverController != nil)  // if the popover is showing (replace with your own test if you wish)
      {
        [self.myPopoverController presentPopoverFromRect:attachmentRect
                                                  inView:myView
                                permittedArrowDirections:UIPopoverArrowDirectionUp
                                                animated:YES];
      }    
    }
    

    在上面, self.myPopoverController nil ,这样我就可以检查它是否为“非”- 无

    但是,请注意,在进行旋转之前,不需要关闭popover。再来一次同样的流行音乐(在这里保存对popover的引用非常方便。)

    在您的例子中,popover是从工具栏按钮发出的,您可以使用如下内容:

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
      // if the popover is showing, adjust its position after the re-orientation by presenting it again:
      if (self.myPopoverController != nil)  // if the popover is showing (replace with your own test if you wish)
      {
        [self.myPopoverController presentPopoverFromBarButtonItem:barButtonItem
                                         permittedArrowDirections:UIPopoverArrowDirectionAny
                                                         animated:YES];
      }    
    }
    
        4
  •  3
  •   sdsykes    15 年前

    如果您只需使用presentPopoverFromBarButtonItem方法来呈现您的popover,则当设备旋转时,popover将自动移动到新按钮位置的正确位置。

        5
  •  2
  •   Dan Bennett    15 年前

    你是从巴布托尼姆还是从rect给PresentPoover打电话?你对轮换上的巴布托石有什么改变吗?

    苹果的文档特别指出,你需要管理FromRect的旋转位置,或者如果你修改了barbutton项。见本报告第四段 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPopoverController_class/Reference/Reference.html

        6
  •  2
  •   James Jones    13 年前

    同样的问题我也碰到过好几次。我通常只做一个方法来显示popover,如下所示:

    - (void) showPopoverForSize:(CGSize) size center:(CGPoint) center {
       CGFloat width = size.width;
        CGFloat height = size.height;
        CGFloat x = center.x - width / 2;
        CGFloat y = center.y - height / 2;
        CGRect frame = CGRectMake(x, y, width, height);
        popover.popoverContentSize = frame.size;
    
        [popover presentPopoverFromRect:frame inView:self.view permittedArrowDirections:0 animated:YES];
    }
    

    接下来我会:

    - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
        [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
        if (popover.isPopoverVisible)
            [self showPopoverForSize:popover.popoverContentSize center:self.view.center];
    }
    

    这将把popover放在任何方向的中心。

        7
  •  1
  •   Pawel Molodkin    10 年前

    在方向更改开始时,关闭弹出窗口,在方向更改完成后,再次关闭弹出窗口并在屏幕上显示其位置:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
                [_popover dismissPopoverAnimated:YES];
            }
        }
    
        - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
        {
            if (_popover) {
                [_popover presentPopoverFromRect:frameRect
                                                          inView:self.view
                                        permittedArrowDirections:UIPopoverArrowDirectionUp
                                                        animated:YES];
            }    
        }
    
        8
  •  0
  •   Vahid    9 年前

    正在执行popover功能:

    func presentPopover() {
        self.popoverFlag = true
        //Presenting PopOver code goes here
        // ...
    }
    

    在改变方向时拒绝提供popover:

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    
            if self.isKindOfClass(ViewController) && self.popoverFlag{
                guard self.presentedViewController != nil else { return }
                dispatch_async(dispatch_get_main_queue()) {
                    self.presentedViewController!.dismissViewControllerAnimated(true, completion: nil)
                }
            }
        }
    

    func popoverPresentationController(popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverToRect rect: UnsafeMutablePointer<CGRect>, inView view: AutoreleasingUnsafeMutablePointer<UIView?>) {
        self.presentPopover()
    }
    
        9
  •  0
  •   Bionicle    8 年前

    应使用UIPopoverPresentationControllerDelegate方法:

    func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>, in view: AutoreleasingUnsafeMutablePointer<UIView>)
    

    见@Hugo Alonso的答案 here