首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ios 9 UIPopoverPresentationController常量旋转放置

ios 9 UIPopoverPresentationController常量旋转放置
EN

Stack Overflow用户
提问于 2016-09-09 15:49:13
回答 1查看 1.3K关注 0票数 0

在iOS9中,当我用UIPopoverPresentationController旋转屏幕时,坐标会被重置为0,而不是附加到作为按钮的sourceView上。

我试过:

popoverPresentationController(popoverPresentationController: UIPopoverPresentationController,willRepositionPopoverToRect rect: UnsafeMutablePointer,inView视图: AutoreleasingUnsafeMutablePointer) { rect.initialize(CGRectMake(200,200,400,400)) }

但没有用。有什么帮助吗?

EN

回答 1

Stack Overflow用户

发布于 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闭包中,您已经有了新的布局,您可以从这些布局中得到约束计算。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39415364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档