gpt4 book ai didi

UIViewController 生命周期断了 iOS13,makeKeyAndVisible() 好像不能操作?

转载 作者:行者123 更新时间:2023-12-03 14:48:26 25 4
gpt4 key购买 nike

我有一个自定义的 UIStoryboardSegue,可以在 iOS12.* 中按需要工作。

目标 View Controller 之一是 UITabbarController:对于每个选项卡,我都有一个嵌入在导航 Controller 中的 Controller 。

不幸的是,对于 iOS13.*,这不能很好地工作: View Controller 生命周期被破坏,并且不再调用 viewXXXAppear() 和 willTransition() 方法。

看起来 makeKeyAndVisible() 没有效果?!

在底部查看屏幕 UI 在没有调用 viewWillAppear() 的情况下是如何困惑的。

一个可怕的临时解决方法

我不得不拉我的头发,但是,我找到了一个公开的修复程序(我必须即时添加导航 Controller )。

这弄乱了 vc 层次结构:你有更好的解决方案吗?

public class AladdinReplaceRootViewControllerSegue: UIStoryboardSegue {
override public func perform() {

guard let window = UIApplication.shared.delegate?.window as? UIWindow,
let sourceView = source.view,
let destinationView = destination.view else {
super.perform()
return
}

let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height

destinationView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)

window.insertSubview(destinationView, aboveSubview: sourceView)


// **My fix**
if #available(iOS 13,*) {
// I introduced an invisible navigation controller starting in iOS13 otherwise, my controller attached to the tabbar thru a navigation, dont work correctly, no viewXAppearis called.
let navigationController = UINavigationController.init(rootViewController: self.destination)
navigationController.isNavigationBarHidden = true
window.rootViewController = navigationController
}
else {
window.rootViewController = self.destination
}

window.makeKeyAndVisible()
}
}

UI destroyed

最佳答案

感谢 Unbalanced calls to begin/end appearance transitions with custom segue,我找到了解决方案
这里发生的事情是目标 View Controller 的创建和附加发生了两次,第一个发生得太快了。
所以你需要做的是:

public class AladdinReplaceRootViewControllerSegue: UIStoryboardSegue {
override public func perform() {

guard let window = UIApplication.shared.delegate?.window as? UIWindow,
let sourceView = source.view,
let destinationView = destination.view else {
super.perform()
return
}

let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height

let mock = createMockView(view: desination.view)
window.insertSubview(mock, aboveSubview: sourceView)

//DO SOME ANIMATION HERE< MIGHT NEED TO DO mock.alpha = 0

//after the animation is done:
window.rootViewController = self.destination
mock.removeFromSuperview()
}

func createMockView(view: UIView) -> UIImageView {
UIGraphicsBeginImageContextWithOptions(view.frame.size, true, UIScreen.main.scale)

view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()
return UIImageView(image: image)
}
}

关于UIViewController 生命周期断了 iOS13,makeKeyAndVisible() 好像不能操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58360026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com