Hello everyone,
I am trying to edit a tab bar controller to display more items. I am using a UIWindow but it just does not work. I have followed some tutorials and one thing I noticed is that in everyone of the the window property is already there.
In my version of Xcode it is not. I have to declare it myself.
Can you help me? Here is the code I am using.
It always prints "exits with error". 😟
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//Window em que a aplicação está a ser executada.
//Função chamada pelo iOS quando a app termina de carregar.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let tabBarController = window?.rootViewController as? UITabBarController {
//Neste projeto, se repararmos no storyboard o primeiro ecrã que vemos faz referência a um tab bar controller, por isso está tudo dentro dele.
//Por isso é que o default view controller é um tab bar controller.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(identifier: "NavController")
//Adicionamos um tabBarItem ao view controller.
viewController.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 1)
//The default value of this property is nil. When configuring a tab bar controller, you can use this property to specify the content for each tab of the tab bar interface.
tabBarController.viewControllers?.append(viewController)
}
else {
print("Exits with error.")
}
return true
}
Probably window is nil here ; test it:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print(window)
if let tabBarController = window?.rootViewController as? UITabBarController {
Do you have a scene delegaye in your app ?
In this case, it is normal, window is nil in AppDelegate.
Need to implement your code in
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
Take care, only available for iOS 13 and over.
Below iOS 13, still need to do in AppDelegate.