在增加preferredContentSize

时间:2019-04-30 01:03:47

标签: ios swift xcode autolayout uikit

我正在使用UIPopoverPresentationViewController在弹出窗口中以模态形式显示UIViewController。根据{{​​3}},如果preferredContentSize的{​​{1}}属性在弹出窗口中显示,则对其进行动画处理。

虽然大小更改有效,但UIViewController的某些值将在动画过程中引起这种奇怪的白色矩形伪像。除了出现这个奇怪的白色矩形外,动画效果还不错。

Apple's documentation

这是我用来显示弹出窗口的代码。在这种情况下,设置为preferredContentSize的{​​{1}}导致工件出现。如果我使用preferredContentSize,那么一切都会正常进行,而且不会产生任何假象。

CGSize(width: 200, height: 100)

出现的工件与CGSize(width: 200, height: 200)之间似乎存在某种关系,但是我无法理解这种关系是什么。

我尝试将@objc private func presentPopover() { let vc = UIViewController() vc.modalPresentationStyle = .popover vc.preferredContentSize = CGSize(width: 200, height: 50) guard let popVC = vc.presentationController as? UIPopoverPresentationController else { fatalError("View Controller must have a Popover Presentation Controller") } popVC.sourceView = self.view popVC.sourceRect = self.label.frame popVC.delegate = self popVC.permittedArrowDirections = .up self.present(vc, animated: true) // very short delay to allow popover growth to animate // without delay, layout happens so quickly that no animation is visible Timer.scheduledTimer(withTimeInterval: 0.15, repeats: false) { (timer) in vc.preferredContentSize = CGSize(width: 200, height: self.popoverHeight) } } func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle { return .none } func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle { return .none } 安排在将来的0.1秒(例如1秒,5秒)之后,以为可能存在一些布局竞争条件,但并不能解决问题。如果没有preferredContentSize,则弹出框会自动显示在最后的Timer上,而不会设置动画。我认为发生这种情况是因为布局发生得如此之快。使用其他延迟构造,例如Timer也会导致白色矩形伪像出现。

0 个答案:

没有答案