我试图用一个选项卡栏切换到另一个根视图控制器;通过app delegate,我想添加过渡动画.默认情况下,它只显示视图,不显示任何动画.

let tabBar = self.instantiateViewController(storyBoard: "Main", viewControllerID: "MainTabbar")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.main.bounds)
appDelegate.window?.rootViewController = tabBar
appDelegate.window?.makeKeyAndVisible()

这就是我切换到另一个rootview控制器的方式.

推荐答案

您可以使用UIView.transition(with: view)替换UIWindow中的rootViewController:

guard let window = UIApplication.shared.keyWindow else {
    return
}

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabbar")

// Set the new rootViewController of the window.
// Calling "UIView.transition" below will animate the swap.
window.rootViewController = vc

// A mask of options indicating how you want to perform the animations.
let options: UIView.AnimationOptions = .transitionCrossDissolve

// The duration of the transition animation, measured in seconds.
let duration: TimeInterval = 0.3

// Creates a transition animation.
// Though `animations` is optional, the documentation tells us that it must not be nil. ¯\_(ツ)_/¯
UIView.transition(with: window, duration: duration, options: options, animations: {}, completion:
{ completed in
    // maybe do something on completion here
})

Swift相关问答推荐

JSON如何解码空对象

SwiftUI轨迹绘制怪异

SwiftUI map 旋转

我在SwiftUI中的动画无法正常工作

SWIFT异步/等待,多个监听程序

SwiftUI,如何更改具有多个按钮和一个布尔条件的一个按钮标签

URL appendPathComponent(_:) 已弃用?

从 Swift Vapor Server 中调用 ShazamKit

try 制作一个 Swift 扩展来替换字符串中的多次出现

符合协议 - 错误?

如何有条件地依赖桌面与 iOS 的系统库?

如何在填充上添加 if else 语句? SwiftUI

(SwiftLint)如果有正文,如何编写规则(可能是自定义),在{之后总是\n(换行)?

VStack SwiftUI 中的动态内容高度

在swift 3中将文件保存在文档目录中?

是否可以在 Swift 中创建通用闭包?

Swift 3.0 的 stringByReplacingOccurencesOfString()

在 Swift for iOS 中沿圆形路径绘制文本

在 Swift 中以编程方式更新约束的常量属性?

使用 JSONEncoder 对类型为 Codable 的变量进行编码