Why during first boot my navigation bar is under status bar - swift

Why when I create navigation controller with empty view controller I receive strange behaviour with navigation bar?
This is how I create navigation controller.
init(
window: UIWindow,
keystore: Keystore,
navigationController: UINavigationController = UINavigationController()
) {
self.navigationController = navigationController
self.keystore = keystore
super.init()
window.rootViewController = navigationController
window.makeKeyAndVisible()
}
Than I simply do :
func start() {
let x = UIViewController()
x.view.backgroundColor = UIColor.red
navigationController.setViewControllers([x], animated: true)
}
And I receive:
But in the next app launches all is fine.

This might be the case, because you set the NavigationController after the app has already started.
I would recommend to set the NavigationController in your AppDelegate.
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let firstViewController = UIViewController()
let navigationController = UINavigationController(rootViewController: firstViewController)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}
}

Related

Instantiate initial UIViewController in AppDelegate

I know, there are a lot of similar threads but I'm still struggling to get this problem solved.
I want to instantiate a certain viewcontroller in AppDelegate (only if a user is not logged in). But even without an authentication check my code in app delegate to instantiate my login controller seems to be ignored:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let loginController = UIStoryboard(name: "Main", bundle: .main).instantiateViewController(withIdentifier: "loginController") as! LoginController
self.window!.rootViewController = loginController
self.window!.makeKeyAndVisible()
return true
}
}
No matter which view controller I instantiate in app delegate, the initial view controller set in storyboard appears when launching my app. (Note: if no view controller is set as initial in storyboard, the app is launching with a black screen).
What's my mistake?
Thanks a lot in advance
Alex
You can try this code in didFinishLaunchingWithOptions method --
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialVC: ViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let navigationController = UINavigationController(rootViewController: initialVC)
navigationController.isNavigationBarHidden = true
self.window?.rootViewController = navigationController self.window?.makeKeyAndVisible()
In a modern iOS app, the app delegate has no window. You can create one but it will be replaced. The visible window belongs to the scene delegate. You need to create the interface using scene delegate code.

How to navigate to a UIViewController programmatically?

I am having difficulties navigating to a UIViewController programmatically from within another UIViewController. I am using UIKit and Swift 5.
I have tried to follow this Medium article but can't get it to work for me.
In my AppDelegate file I have changed the didFinishLaunchingWithOptions function to this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = LoginViewController()
self.window?.makeKeyAndVisible()
return true
}
And then in my LoginViewController() I attempt to navigate to my DashboardViewController by doing the following:
override func viewDidLoad() {
super.viewDidLoad();
// present a modal with an embed UINavigationController
let rvc = DashboardViewController()
let vc = UINavigationController(rootViewController: rvc)
vc.modalPresentationStyle = .overFullScreen
present(vc, animated: true, completion: nil)
}
But DashboardViewController is never called.
What am I doing wrong and how can I change the code to work?
Thank you.
Try this in your viewDidLoad method:
let rvc = self.storyboard!.instantiateViewControllerWithIdentifier("DashboardViewController") as! DashboardViewControllerSwift
let navController = UINavigationController(rootViewController: rvc)
navController.modalPresentationStyle = .overFullScreen
self.present(navController, animated:true, completion: nil)
Change DashboardViewControllerSwift to the name of your Swift file for your DashboardViewController, without the .swift bit. Make sure it is set as the class for DashboardViewController object in IB/Storyboard.
Also ensure the DashboardViewController object in IB/Storyboard has its Storyboard Identifier set as DashboardViewController.
1- start with SceneDelegate
self.window = UIWindow(windowScene: windowScene)
let navigation = UINavigationController()
window.rootViewController = navigation
window?.makeKeyAndVisible()
2-then
let rvc = DashboardViewController()
self.navigationController?.pushViewController(rvc, animated: true)
3- try it in viewDidLoad() if not work try it in viewWillAppear
I ended up coming across a Youtube video that helped me solve my issue.
AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let login = LoginViewController()
let navigationController = UINavigationController(rootViewController: login)
navigationController.setNavigationBarHidden(true, animated: true)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
LoginViewController
let dashboard = DashboardViewController()
self.navigationController?.pushViewController(dashboard, animated: true)

AppDelegate not showing Storyboard buttons

I am setting up a MainViewController holding multiple ViewControllers the following way:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let pageController = UIPageViewController(
transitionStyle: UIPageViewControllerTransitionStyle.scroll,
navigationOrientation: UIPageViewControllerNavigationOrientation.horizontal,
options: nil
)
let navigationController = MainViewController(rootViewController: pageController)
navigationController.view.backgroundColor = UIColor.white
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let leftView = storyboard.instantiateViewController(withIdentifier: "VC1")
let middleView = storyboard.instantiateViewController(withIdentifier: "VC2")
let rightView = storyboard.instantiateViewController(withIdentifier: "VC3")
navigationController.viewControllerArray = [leftView, middleView, rightView]
self.window!.rootViewController = navigationController
self.window!.makeKeyAndVisible()
return true
}
}
My MainViewController also has what is supposed to be a floating button. I have aded this in the storyboard (see picture below). The problem is that the button is not showing. How do i make it show?
I know that somehow this problem is related to the fact that I adding MainViewController in the AppDelegate, but I am not sure how that is hiding the buttons.
You aren't creating your MainViewController instance from the storyboard, you are just creating it with a call to its initialiser. This means that none of your storyboard elements will be loaded
You need something like:
let navigationController = storyboard.instantiateViewController(withIdentifier: "Main")
navigationController.viewControllers = [pageController]

UINavigationBar not showing

I have a swift project that is programmatic and segues from a mapview to another view. After the segue the navigation bar is not present. Since the files were copied over from an earlier project where this doesn't happen I'm perplexed. It should be very straight forward.
In AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let homeViewController = MapViewController()
self.navigationController = UINavigationController()
self.navigationController?.setNavigationBarHidden(false, animated: false)
navigationController?.viewControllers = [homeViewController]
self.window!.rootViewController = navigationController
self.window?.makeKeyAndVisible()
let attributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes
return true
}
And the segue code called in the MapViewController:
let storeViewController = ViewController()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.navigationController?.setNavigationBarHidden(false, animated: false)
appDelegate.navigationController?.pushViewController(storeViewController, animated: true)
You have to embedded MapViewController in UINavigationController and Push ViewControllers as needed
to hide use self.navigationController?.isNavigationBarHidden = false
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let homeViewController = MapViewController()
let navController = UINavigationController(rootViewController: homeViewController)
self.window!.rootViewController = navController
self.window?.makeKeyAndVisible()
let attributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes
return true
}
and in MapViewController
To navigate:
let storeViewController = ViewController()
self.navigationController?.pushViewController(storeViewController, animated: true)
//if you want to hide or show navigation
//self.navigationController?.isNavigationBarHidden = false // true

how to create very first view for Swift 3

Hi I have made a ViewController.
But I don't know how to add very first view right after an app is tapped.
I added below codes in ViewController.swift
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "FirstViewController")
self.present(controller, animated: true, completion: nil)
the FirstViewController appears after the app tapped and shows white background for about a second.
When you first launch your app, the LaunchScreen.storyboard is displayed, as a transition while your app is loading. You can customise this in Interface Builder.
check the option 'is initial view Controller' for that firstViewController from the storyboard.
You can set initial view controller programmatically or via storyboard
Method 1:programmatically--
Set storyboard-identifire for Viewcontroller in Main.storyboard file
like
After that set rootviewController in Appdelegate class.
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let redViewController = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
window?.rootViewController = redViewController
window?.makeKeyAndVisible()
return true
}
}
For setting up with navigation controller use UINavigationController -
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryBoard.instantiateViewController(withIdentifier: "viewController") as! viewController
let navigationController = UINavigationController(rootViewController: viewController)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}
Method 2:Via storyboard--
Go to respective storyboard & select Is Initial View Controller