Use of undeclared type "ViewController" - ios

I'm using a library for Tap Bar Controller so I have to change the name of the class controller to RAMAnimatedTabBarController to use the features. But in app delegate when I want to present the main bar it displays this error
Use of undeclared type "RAMAnimatedTabBarController"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if UserDataSingleton.sharedDataContainer.logged == "logged" {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "Order") as! RAMAnimatedTabBarController // error here
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}else{
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "loginViewController") as! loginViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}

You need to import it first. At the beginning of the file, use:
import RAMAnimatedTabBarController

Related

new ViewController is not displaying when pushed in AppDelegate [duplicate]

This question already has an answer here:
Xcode 11 & iOS13, using UIKIT can't change background colour of UIViewController
(1 answer)
Closed 3 years ago.
Here is my code to present a ViewController from app delegate in swift 5, I have done everything the same as everyOne else But the new ViewController is not shown when app is launched.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let welcome = mainStoryboardIpad.instantiateViewController(withIdentifier: "Welcome") as! WelcomeScreen
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = welcome
self.window?.makeKeyAndVisible()
return true
}
Put it in
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let welcome = mainStoryboardIpad.instantiateViewController(withIdentifier: "Welcome") as! WelcomeScreen
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = welcome
self.window?.makeKeyAndVisible()
return true
}
UPDATE:
If it is not working try:
if let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main){
if let welcome = mainStoryboardIpad.instantiateViewController(withIdentifier: "Welcome") as! WelcomeScreen{
//Does it come here?
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = welcome
self.window?.makeKeyAndVisible()
}
}
Make sure you actually get in to the body if most inner if which is if let welcome = mainStoryboardIpad.instantiateViewController(withIdentifier: "Welcome") as! WelcomeScreen

View controller added in app delegate not appearing

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let auth: UIViewController =
storyBoard.instantiateViewController(withIdentifier: "Auth") as UIViewController
window?.rootViewController?.present(auth, animated: true, completion: nil)
return true
}
I get the error...
Warning: Attempt to present
on whose view is not in the
window hierarchy!
I presume the root controller has not been properly configured at this point in the app life cycle.
How do I do this? I want to avoid having the root controller having to check whether it needs to show the login screen.
You can do it like that:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if Settings.appSettings.authToken != nil {
self.showMainController()
}
NotificationCenter.default.addObserver(forName: .authorizationOperationDidSuccess,
object: nil, queue: nil) { (notification) in
self.showMainController()
}
return true
}
private func showMainController() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller: UIViewController =
storyBoard.instantiateViewController(withIdentifier: "Main") as UIViewController
if self.window == nil {
self.window = UIWindow(frame: UIScreen.main.bounds)
}
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
}
private func showAuthorizationController() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller: UIViewController =
storyBoard.instantiateViewController(withIdentifier: "Auth") as UIViewController
if self.window == nil {
self.window = UIWindow(frame: UIScreen.main.bounds)
}
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
}
On successful login make
NotificationCenter.default.post(name: .authorizationOperationDidSuccess,
object: nil)
Make change here,
let auth: AuthVC = storyBoard.instantiateViewController(withIdentifier: "Auth") as AuthVC
// AuthVC is your_VC_name
Still facing issue, you can ask.

iOS Empty Space above the navigation bar?

When I check and calling the Viewcontroller from Appdelegate my app shows empty space above navigation bar I don't know what is the reason.
Here I give the code and screen shot.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let checkUserid = UserDefaults.standard.value(forKey: "USERID")
if checkUserid != nil {
print(checkUserid!)
let mainStoryboard = UIStoryboard(name: "Main" , bundle: nil)
let revealViewController = mainStoryboard.instantiateViewController(withIdentifier: "RevealViewController") as? SWRevealViewController
let navigationController = UINavigationController(rootViewController: revealViewController!)
navigationController.navigationBar.isTranslucent = false
window!.rootViewController = navigationController
window!.makeKeyAndVisible()
}
else
{
let mainStoryboard = UIStoryboard(name: "Main" , bundle: nil)
let loginViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let navigationController = UINavigationController(rootViewController: loginViewController)
navigationController.navigationBar.isTranslucent = false
window!.rootViewController = navigationController
window!.makeKeyAndVisible()
}
}
I believe you need to hide the default navigation bar.
Try adding the following code:
navigationController.isNavigationBarHidden = true

How can I change the initial ViewController in LaunchScreen with Swift?

I want to check whether the user is logged in or not, if the user is logged in, then bring him to main screen, or show the welcome screen.
You can't do in Launch Screen, but you can achieve same in AppDelegate's method didFinishLauchingWithOption, there you can check if user logged in or not and set the root view controller and don't set initialViewController in storyboard.
It should be something like this
NSString *identifier = isLoggedIn ? #"IdentifierForVC1" : #"IdentifierForVC2";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier: identifier];
self.window.rootViewController = vc;
Code is not tested in an editor may have some
Swift code should be like this
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier(identifier) as! UIViewController
self.window?.rootViewController = vc
Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "Identifier")
let navigationController = UINavigationController(rootViewController: viewController)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
You can't do it in Launch Screen but can do in AppDelegate
for Swift 4
if userLoggedIn == True {
//Do something
} else {
//Do something
}
UserDefaults.standard.set(value:Bool ,forKey:"loggedIn") //This will save the bool value to your UserDefaults
}
Now Go to your AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
if (UserDefaults.standard.bool(for key: "loggedIn")) == true {
let welcomeVC = mainStoryboard.instantiateViewController(withIdentifier: "welcomeVC") as! WelcomeVC
self.window?.rootViewController = newRootVC
self.window?.makeKeyAndVisible()
} else {
let loginVC = mainStoryboard.instantiateViewController(withIdentifier: "loginVC") as! LoginVC
self.window?.rootViewController = newRootVC
self.window?.makeKeyAndVisible()
}
return true
}
Happy Coding
Hope this helps :)
Write this code in AppDelegate Swift 4.0
In didFinishLaunchingWithOptions pass your viewController to self.launch(rootController: ViewController())
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let frame = UIScreen.main.bounds
self.window = UIWindow(frame: frame)
self.window!.rootViewController = ViewController()
self.window!.makeKeyAndVisible()
return true
}
Write this code in AppDelegate Swift 5.0 Xcode 11.0
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
if let windowScene = scene as? UIWindowScene{
let window = UIWindow(windowScene: windowScene)
let rootViewController = UIStoryboard(name: "Auth", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! UINavigationController
window.rootViewController = rootViewController
self.window = window
window.makeKeyAndVisible()
}
}
In Swift, in your AppDelegate.swift,
self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("{INSERT_STORYBOARD_ID_HERE}") as? UIViewController

How to set UITabBarController when setting initial view controller in app delegate

I need to programatically set initial view controller (based on user login status).
This is my initial code:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainRootController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as UIViewController
if self.window != nil {
let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as UINavigationController
navigationController.viewControllers = [mainRootController]
self.window!.rootViewController = navigationController
}
return true
This loads 'MainViewController' fine. But I am missing UITabBarController also and I don't know how to set it up programatically here.
Any help?
Try this one:
let tabBar = UITabBarController()
tabBar.viewControllers = [navigationController]
self.window!.rootViewController

Resources