我的问题是我无法找到正确的代码来以编程方式在 IOS 中的两个场景之间进行转换。我尝试了一些示例代码,但没有一个按预期工作。
let vc storyboard!.instantiateViewController(withIdentifier: "savedMode") as! SavedModeViewController
let navigationController = UINavigationController(rootViewController: vc)
self.present(navigationController, animated: true, completion: nil)
这段代码进入了新的场景,但是后退按钮没有出现在导航栏的左侧,并且场景之间的转换是错误的;有从下到上的动画,而不是从左到右的动画。
let controller = storyboard?.instantiateViewController(withIdentifier: "savedMode") as! SavedModeViewController
present(controller, animated: true, completion: nil)
这是我尝试过的第二个代码。问题是,使用此代码,导航栏在第二个场景中根本不显示。
我尝试了另一种方法,但不是第二个场景,屏幕只是变黑了。
最佳答案
调用 present
将以模态方式在当前 View Controller 上呈现新的 View Controller 。
您想要的是将新的 View Controller 推送到现有的 UINavigationController 上 pushViewController
.
if let savedVC = storyboard?.instantiateViewController(withIdentifier: "savedMode") as? SavedModeViewController,
let navigationController = self.navigationController {
navigationController.pushViewController(savedVC, animated: true)
} else {
// TODO: Handle unexpected nil occurrences
print("Error loading SavedModeViewController")
}
关于ios - 以编程方式在场景之间进行转换的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57233252/