【发布时间】:2020-01-29 07:57:01
【问题描述】:
我有很多视图控制器使用相同的两个功能来为我显示和隐藏弹出窗口。每次我使用它们时,我都会问自己,将它们放在一个名为 PopupUtils 的全局类中,并将这些函数设置为静态函数是否会更好。
我做到了,它成功了,但我不确定这是否是一件好事,因为我必须将三个参数传递给我的函数:父视图控制器、子视图控制器和 popup_container 视图
既然都是val传的,是不是内存有问题?还是我应该注意的任何其他问题?
这是我的名为 Popup Utils 的静态类
class PopupUtils {
static func showPopupView(parentViewController: UIViewController, childViewController: UIViewController, popupContainer: UIView) {
parentViewController.addChild(childViewController)
popupContainer.addSubview(childViewController.view)
childViewController.view.frame = popupContainer.bounds
childViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
childViewController.didMove(toParent: parentViewController)
UIView.transition(with: popupContainer, duration: 0.2, options: .transitionCrossDissolve, animations: {
popupContainer.isHidden = false
})
}
static func removePopupView(childViewController: UIViewController, popupContainer: UIView){
// Remove pop up VC from children
childViewController.willMove(toParent: nil)
childViewController.view.removeFromSuperview()
childViewController.removeFromParent()
// Hide pop up container
popupContainer.isHidden = true
// Release language menu
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "releaseMenuSwipe"), object: nil)
}
}
【问题讨论】:
-
为什么不显示弹出窗口而不是嵌入它?
-
我不知道还有其他解决方案。如何呈现弹出窗口?
-
stackoverflow.com/questions/34149386/… 这对于静态函数可能会有所帮助。
-
您可以在 Storyboard 中创建新的 UIViewController,并从前一个屏幕呈现。然后选择你的 Previous ViewController -> Attribute Inspector -> Set transition Style to "cross溶解"。并将演示文稿设置为“当前内容”。
-
如果你将 popupVC 的
modalPresentationStyle设置为.overFullScreen然后呈现它之前的 VC 将是可见的,只要确保 popupVC 的背景是透明的