iOS Empty Space above the navigation bar? - ios

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

Related

How to go to the what I want controller when application didFinishLaunching

When I launching my application, I want go to the custom view controller, but the default first controller in storyboard:
I want go to brown vc when launch my app:
My method is below, but get a black screen with Unable to capture view hierarchy
1) In Info.plist, delete the Main storyboard file base name line.
2) In my Appdelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let sb:UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let nav: CustomNavController = sb.instantiateViewController(withIdentifier: "CustomNavController") as! CustomNavController
let red_vc:ViewController = sb.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let green_vc:ViewController2 = sb.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
let brown_vc:ViewController3 = sb.instantiateViewController(withIdentifier: "ViewController3") as! ViewController3
nav.viewControllers = [red_vc, green_vc, brown_vc]
self.window? = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = nav
self.window?.makeKeyAndVisible()
return true
}
Result get a black screen:
Is which step I mistake? and how can I get it well ? When launch my application I want go to brown vc.
ATTEMPT - 1
Using push to brown vc, it is not work too:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let sb:UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let nav: CustomNavController = sb.instantiateViewController(withIdentifier: "CustomNavController") as! CustomNavController
let red_vc:ViewController = sb.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let green_vc:ViewController2 = sb.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
let brown_vc:ViewController3 = sb.instantiateViewController(withIdentifier: "ViewController3") as! ViewController3
//nav.viewControllers = [red_vc, green_vc, brown_vc]
nav.pushViewController(red_vc, animated: false)
nav.pushViewController(green_vc, animated: false)
nav.pushViewController(brown_vc, animated: false)
self.window? = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = nav
self.window?.makeKeyAndVisible()
return true
}
You no need delete Main storyboard file base name in Info.plist
In AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let mainStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let brownVC = mainStoryboard.instantiateViewController(withIdentifier: "brownVCIdentifier") as! BrownViewController
let navigationController = application.windows[0].rootViewController as! UINavigationController
navigationController.isNavigationBarHidden = true
navigationController.pushViewController(brownVC, animated: false)
return true
}
(or)
In RedViewController(first viewController):
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let brownVC = storyboard?.instantiateViewController(withIdentifier: "brownVCIdentifier") as! BrownViewController
navigationController?.isNavigationBarHidden = true
navigationController?.pushViewController(brownVC, animated: false)
}
Github link:
https://github.com/k-sathireddy/DirectNavigationSample
You are setting the root viewcontroller as nav which has no UIView and it's creating a blank view due to which you see the black view when app is launched.
this code nav.viewControllers = [red_vc, green_vc, brown_vc] is not making any sense as you cannot set view controllers like this for UINavigationController. Navigation controller works with PUSH/POP mechanism hence you need to push the view controllers on nav controller which will correctly setup the view hierarchy. Push the controllers to brown_vc in nav and it should work.

Launch ViewController from AppDelegate, tabbar missing from bottom

I am developing my first app, where I am trying to manage a session in my app, where I am trying to check if user has logged in recently. If user has logged in recently, then I want to skip login page, and move him to next page.
Here what I am doing, however I am unable to proceed forward
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewControllerB = mainStoryboard.instantiateViewControllerWithIdentifier("account") as! AccountDetails
let navController = UINavigationController(rootViewController: viewControllerB)
let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
appDelegate.window?.rootViewController = viewControllerB
let vc = self.window?.rootViewController
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(navController, animated: true, completion: nil)
return true
}
I am able to get to the desired view,but the Tabbar on that view is missng. I want to restore the same.
Second Screen (My Account which I want to show)
class AccountDetails: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Initialize Tab Bar Item
tabBarItem = UITabBarItem(title: "Account Details", image: UIImage(named: "Account.png"), tag: 1)
}
}
Replace the line self.window?.rootViewController?.presentViewController(navController, animated: true, completion: nil)
with
self.window?.rootViewController = navController
Change window's rootViewController:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if(User loggin last time) {
let yourTargetViewController = UIViewController()
yourTargetViewController.view.backgroundColor = UIColor.redColor()
self.window?.rootViewController = yourTargetViewController
}
return true
}
This is the working solution
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewControllerB = mainStoryboard.instantiateViewControllerWithIdentifier("tabbar") as! UITabBarController
let navController = UINavigationController(rootViewController: viewControllerB)
let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
appDelegate.window?.rootViewController = viewControllerB
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(navController, animated: true, completion: nil)
//tabbar is the storyboard ID of tabbarcontroller

iOS Swift SlideMenuControllerSwift

I am learning to develop iOS and come across to this awesome library SlideMenuController. I am able to make use of this library to create awesome slide menu easily.
However, there are some problem that I am facing during implementing with this library.
Environment
In my project storyboard, I have 1 TabBarViewController and 2 ViewController which is under this TabBarViewController. I also have 2 independent ViewController which are LoginViewController and LeftMenuViewController.
I have below code in my AppDelegate.swift file
...
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewControllerWithIdentifier("TabBarViewController") as! TabBarViewController
let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftMenuViewController") as! LeftMenuViewController
let slideMenuController = SlideMenuController(mainViewController: mainViewController, leftMenuViewController: leftViewController)
SlideMenuOptions.contentViewScale = 1
SlideMenuOptions.hideStatusBar = false;
self.window?.rootViewController = slideMenuController
self.window?.makeKeyAndVisible()
return true
}
...
Everything work perfectly if I don't need my login view as a initial view. I notice that this line of code self.window?.rootViewController = slideMenuController will always make sure that view in mainViewController to be the initial view when the app open.
How can I have my LoginView as the initial view when my app start up and the slide menu is still bind to the TabBarViewController and working properly?
I have try to move the code from AppDelegate.swift to viewDidLoad() function in TabBarViewController.swift but no luck. It is not working.
Need some guilds and help on this. Thanks.
you can use this code instead of yours :
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewControllerWithIdentifier("TabBarViewController") as! TabBarViewController
let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftMenuViewController") as! LeftMenuViewController
let loginViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
let nvc: UINavigationController = UINavigationController(rootViewController: loginViewController)
leftViewController.mainVC = nvc
let slideMenuController = SlideMenuController(mainViewController: nvc, leftMenuViewController: leftViewController)
SlideMenuOptions.contentViewScale = 1
SlideMenuOptions.hideStatusBar = false;
self.window?.rootViewController = slideMenuController
self.window?.makeKeyAndVisible()
return true
}
dont forget to add this variable in your LeftMenuViewController
var mainVC : UIViewController!

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