在iOS9中,当我用UIPopoverPresentationController旋转屏幕时,坐标会被重置为0,而不是附加到作为按钮的sourceView上。
我试过:
popoverPresentationController(popoverPresentationController: UIPopoverPresentationController,willRepositionPopoverToRect rect: UnsafeMutablePointer,inView视图: AutoreleasingUnsafeMutablePointer) { rect.initialize(CGRectMake(200,200,400,400)) }
但没有用。有什么帮助吗?
发布于 2016-10-15 22:53:16
您可以将约束应用于sourceView按钮,然后将其用作popover源:
myPopoverViewController.popoverPresentationController?.sourceView = button
您也可以在故事板中设置sourceView,但是需要在代码中设置sourceRect:
myPopoverViewController.popoverPresentationController?.sourceRect = button.bounds
您需要正确的源rect为弹出箭头正确对齐。现在,你不需要解散和提出的波波夫轮流。UIPopoverPresentationController是为你做的。在设置好创建popover之后,您甚至不需要更新sourceView/sourceRect。
使用viewWillTransition捕捉大小和方向的更改:
覆盖func viewWillTransition(到size: CGSize,with: UIViewControllerTransitionCoordinator) { super.viewWillTransition( to : size,with :coordinator.animate) coordinator.animate(alongsideTransition:{_ in if self.popover != nil { //可选滚动到popover rect,如果在滚动视图中让rect =.self.scrollView.scrollRectToVisible( rect,动画: false) //更新源rect约束buttonConstraint.constant =.button.setNeedsLayout() button.layoutIfNeeded() } },完成:0)}
解释:使用animate(alongsideTransition: ((UIViewControllerTransitionCoordinatorContext) -> Void)?, completion: ((UIViewControllerTransitionCoordinatorContext) -> Void)? = nil)的诀窍是您应该在alongsideTransition闭包中更新约束,而不是在completion中更新约束。通过这种方式,您可以确保在旋转结束时恢复弹出时,UIPopoverPresentationController具有更新的sourceRect。
似乎违反直觉的是,在alongsideTransition闭包中,您已经有了新的布局,您可以从这些布局中得到约束计算。
https://stackoverflow.com/questions/39415364
复制相似问题