首先,这是我的 View Controller /segue 设置:
最右边的三个 View Controller 的背景 View 是 UIVisualEffectViews,源 View Controller 应该通过它们可见。它们被添加到各种 viewDidLoad()
中,如下所示:
let blurEffect = UIBlurEffect(style: .dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.tableView.backgroundView = blurEffectView
现在,通过设置 View Controller 可以看到主视图 Controller (“垃圾日”),但是只要最右边的两个 VC 之一完全显示在屏幕上,设置 VC 就会消失。这是一个屏幕录像:
Screen recording of the source view controller dis- and reappearing
(请忽略这些小故障,我用来上传这个的应用程序显然损坏了视频)
从技术上讲,Show segue 没有“Over Current Context”之类的东西,因此,我不应该期望源 VC 不会消失,但必须有一种方法可以在没有自定义的情况下使它工作继续。
最佳答案
我建议您在 View Controller 之间创建自定义转换。
我刚刚编写并测试了这个类:
class AnimationController: NSObject, UIViewControllerAnimatedTransitioning
{
var pushing = true
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.3
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let duration = transitionDuration(using: transitionContext)
let toVc = transitionContext.viewController(forKey: .to)!
let toView = transitionContext.view(forKey: .to)!
let fromView = transitionContext.view(forKey: .from)!
let container = transitionContext.containerView
if pushing {
container.addSubview(fromView)
container.addSubview(toView)
}
var finalFrame = transitionContext.finalFrame(for: toVc)
if pushing {
finalFrame.origin.x = finalFrame.width
toView.frame = finalFrame
finalFrame.origin.x = 0
} else {
finalFrame.origin.x = finalFrame.width
}
UIView.animate(withDuration: duration, delay: 0, options: .curveEaseOut, animations: {
if self.pushing {
toView.frame = finalFrame
} else {
fromView.frame = finalFrame
}
}) { (_) in
transitionContext.completeTransition(true)
if self.pushing {
container.insertSubview(fromView, belowSubview: toView)
} else {
fromView.removeFromSuperview()
}
}
}
}
在您的 UINavigationController
类中执行以下操作:
class NavigationController: UINavigationController {
let animationController = AnimationController()
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
}
还有这个扩展:
extension NavigationController: UINavigationControllerDelegate
{
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
animationController.pushing = operation == .push
return animationController
}
}
但是,这会使您失去交互式关闭手势(从屏幕左侧滑动以关闭),因此您需要自己修复。
关于ios - 如何防止源 View Controller 在推送后消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47888620/