I am setting a new navigation for my app on launch. But when I launch it appears from a black color animation.After black color it sets it navigation bar. Please tell me what is issue.
I am using below code
var controller = UIViewController()
//App Theming
var navController = UINavigationController()
navController.navigationBar.barTintColor = UIColor.white
navController.navigationBar.tintColor = UIColor.white
navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
navController.navigationBar.shadowImage = UIImage()
navController.navigationBar.setBackgroundImage(UIImage(), for: .default)
navController.navigationBar.isTranslucent = false
navController = UINavigationController(rootViewController: viewcontroller)
navController.navigationBar.isHidden = true
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = navController
appDelegate.window?.makeKeyAndVisible()
The problem is this line:
navController.navigationBar.isHidden = true
Delete it and try again.
Please use snippet bellow
In this I am using ViewController from Main storyboard
// mainStoryboard
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
// rootViewController
let rootViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
// navigationController
let navigationController = UINavigationController(rootViewController: rootViewController!)
//App Theming
navigationController.navigationBar.barTintColor = UIColor.white
navigationController.navigationBar.tintColor = UIColor.white
navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
navigationController.title = "Testing Th"
navigationController.navigationBar.shadowImage = UIImage()
navigationController.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController.navigationBar.isTranslucent = false
navigationController.navigationBar.isHidden = true
// self.window
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.rootViewController = navigationController
self.window!.makeKeyAndVisible()
If I unhide NavigationBar
navigationController.navigationBar.isHidden = false
you can clearly see the results
I can see that you did not give the navigationController any viewControllers.
You need to pass the navigation controller at least one viewController for it to know which viewController to start your
navigation process from, follow the code below:
var window: UIWindow?
let nav = UINavigationController()
1- here is where i declared my Initial viewController (where i want my navigation process to start from)
var main = HomeViewController(nibName: "HomeViewController", bundle: nil)
2- here is where i give the navigationController the first viewController to start from.
window?.rootViewController = nav
nav.viewControllers = [main]//you need to have this line
nav.isNavigationBarHidden = true
window?.makeKeyAndVisible()
alright i just noticed you're using storyboards, give this a try:
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var ivc = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
navigationController?.pushViewController(anIvc, animated: true)
window.rootViewController = ivc
window.rootViewController = navigationController
window.makeKeyAndVisible()
Your code should be placed in
import UIKit
// AppDelegate class file
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Be attentive. controller allocated withou any layout. Change to custom controller class or load from IB resource (storyboard/nib)
let controller = UIViewController()
let navigationController = rootNavigationController
// Setup viewControllers. Just one controller as root
navigationController.viewControllers = [controller]
// You already have a reference to window in your AppDelegate
window.rootViewController = navigationController
window.makeKeyAndVisible()
}
extension AppDelegate {
// Move out of AppDelegate class code to create theming NavigationController
private var rootNavigationController: UINavigationController {
let navController = UINavigationController()
navController.navigationBar.barTintColor = UIColor.white
navController.navigationBar.tintColor = UIColor.white
navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
navController.navigationBar.shadowImage = UIImage()
navController.navigationBar.setBackgroundImage(UIImage(), for: .default)
navController.navigationBar.isTranslucent = false
navController.navigationBar.isHidden = true
// If it theme for all application you should use appearances
/* For Example
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UINavigationBar.appearance().tintColor = .white
*/
return navController
}
}
You possible errors:
controller instance haven't any layout. By default ViewController
haven't anything
You code call from separated place. It's market by you call of AppDelegate instance. Configure you rootViewController in
didFinishLoading
You have an separated ViewController that placed as root
Related
I have been teaching my self how to develop and I got to the point to where I need guidance from experts. I created some of the user interface programmatically using the MVC structure. My question is how can I embed my navigation controller into my tab bar controller so my tab bar controller can be on every screen. I made the tab bar controller in main.storyboard and referenced it in the View controller I named Home Controller.
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tabViewController = storyboard.instantiateViewController(withIdentifier: "TabBar")
self.present(tabViewController, animated: true, completion: nil)
The code above is in the view did load function. I was wondering do I need to change up the root view controller in my app delegates?
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let layout = UICollectionViewFlowLayout()
window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))
This is in my finished launched func.
Use following lines
var navigationController = UINavigationController(rootViewController: viewController));
tabBarController.viewControllers = [navigationController,firstViewControllersecondViewController, ]
create a separate UITabBarController class and initialize it in AppDelegate like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = MyTabBarController()
window?.makeKeyAndVisible()
return true
}
and the custom tabBarController
import UIKit
class MyTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.barTintColor = UIColor.black
self.tabBar.tintColor = UIColor.white
self.tabBar.unselectedItemTintColor = UIColor.white.withAlphaComponent(0.4)
let firstViewController = FirstViewController()
let firstViewTabBarItem = UITabBarItem(title: "First", image: UIImage(named: "calculator"), selectedImage: UIImage(named: "calculator"))
firstViewController.tabBarItem = firstViewTabBarItem
firstViewController.tabBarItem.tag = 0
let historyViewController = HistoricDataViewController()
historyViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .history, tag: 1)
let tabBarList = [calculateViewController, historyViewController]
viewControllers = tabBarList.map{UINavigationController(rootViewController: $0)}
}
}
Hope this example helps.
in my app i am using slider so i am using this library.
https://github.com/dekatotoro/SlideMenuControllerSwift
but while using this i am confused in use of navigation bar.
in my appdelegate i write down this code
window = UIWindow(frame: UIScreen.mainScreen().bounds)
storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller123 : MainViewController = (self.storyboard!.instantiateViewControllerWithIdentifier("MainViewController") as? MainViewController)!
let controllerright : drawerViewController = self.storyboard!.instantiateViewControllerWithIdentifier("drawerViewController") as! drawerViewController
let controllerleft : RightViewController = self.storyboard!.instantiateViewControllerWithIdentifier("RightViewController") as! RightViewController
let controller = SlideMenuController (mainViewController: controller123, leftMenuViewController: controllerleft, rightMenuViewController: controllerright)
navigation = UINavigationController(rootViewController: controller)
window?.rootViewController = navigation
window?.makeKeyAndVisible()
and in my mainviewcontroller i add drwer by this code
slideMenuController()?.addRightBarButtonWithImage(UIImage(named: "ic_menu_black_24dp")!)
because i want drawer on right side
and when i select one item from drawer i write this code
let controller123 : RightViewController = (self.storyboard!.instantiateViewControllerWithIdentifier("RightViewController") as? RightViewController)!
let controllerright : drawerViewController = self.storyboard!.instantiateViewControllerWithIdentifier("drawerViewController") as! drawerViewController
let controllerleft : RightViewController = self.storyboard!.instantiateViewControllerWithIdentifier("RightViewController") as! RightViewController
let controller = SlideMenuController (mainViewController: controller123, leftMenuViewController: controllerleft, rightMenuViewController: controllerright)
navigationController?.pushViewController(controller, animated: false)
but now issue is that i can not set navigation bar title or even i can not hide back button so how can i solve that?
Try something like this. Add this line in your viewDidLoad() method of drawerViewController
self.navigationItem.hidesBackButton = true
self.navigationItem.title = "Your Title"
//If you want to create a custom title View then try this
self.navigationItem.titleView = yourview
Hope this will help you.
try this code "override func viewWillAppear(animated: Bool)" in current view controller :
navigationItem.hidesBackButton = true;
navigationItem.title = "Hello";
hope this code will help you.
Try this in viewDidLoad in each ViewController that navigates from Drawer
self.navigationItem.hidesBackButton = true
self.navigationItem.title = "Title"
I am having app with Slider where I have provided drop down to switch between users. As soon as app user switch to other user I want to reset entire navigation flow and start it from 1st screen.
For e.g
Screen A -> Screen B -> Screen C -> Screen D -> User opens slider and switch users -> Jump to Screen A (Remove other screen from navigation). We can consider gmail app example here, where we can switch between different accounts and gmail app redirect user to primary inbox.
EDIT :
I am using library for Slider menu available on git.
https://github.com/dekatotoro/SlideMenuControllerSwift
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var navigationController: UINavigationController?
var storyboard: UIStoryboard?
var leftViewController: LeftSidePanelViewController?
// var uuid: String?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if Utility.getUserStatus() == 0 {
storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard!.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
leftViewController = storyboard!.instantiateViewControllerWithIdentifier("LeftSidePanelViewController") as? LeftSidePanelViewController
navigationController = UINavigationController(rootViewController: mainViewController)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
leftViewController!.mainViewController = navigationController
let slideMenuController = ExSlideMenuController(mainViewController:navigationController!, leftMenuViewController: leftViewController!)
slideMenuController.automaticallyAdjustsScrollViewInsets = true
slideMenuController.delegate = mainViewController
self.window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
self.window?.rootViewController = slideMenuController
self.window?.makeKeyAndVisible()
Utility.setUUID(UIDevice.currentDevice().identifierForVendor!.UUIDString)
} else {
storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard!.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
leftViewController = storyboard!.instantiateViewControllerWithIdentifier("LeftSidePanelViewController") as? LeftSidePanelViewController
navigationController = UINavigationController(rootViewController: mainViewController)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
leftViewController!.mainViewController = navigationController
let slideMenuController = ExSlideMenuController(mainViewController:navigationController!, leftMenuViewController: leftViewController!)
slideMenuController.automaticallyAdjustsScrollViewInsets = true
slideMenuController.delegate = mainViewController
self.window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
self.window?.rootViewController = slideMenuController
self.window?.makeKeyAndVisible()
}
return true
}
}
I have tried different solutions for this but nothing seems to be working.
var alreadyPushed = false
//Check if the view was already pushed
if let viewControllers = self.navigationController?.viewControllers {
for viewController in viewControllers {
if let viewController = viewController as? HomeViewController {
self.navigationController?.popToViewController(viewController, animated: true);
print(" Push Your Controller")
alreadyPushed = true
break
}
}
}
if alreadyPushed == false {
print("Pushing...")
let YourControllerObject = self.storyboard?.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
self.navigationController?.presentViewController(YourControllerObject, animated: true, completion: nil)
// HERE also tried pushViewController but that was also not working..
self.navigationController?.dismissViewControllerAnimated(false, completion: nil)
}
Solution 2:-
Also tried self.navigationController?.viewController.removeAll() and then push/present the HomeView but that is also not working.
Anyone having any suggestion or tips to solve this.
A very fast way:
// Put this line in the UIViewController where you want to reset navigation
self.navigationController?.viewControllers = [self]
You will erase the view controller stack and reset the navigation.
You can use for it
self.navigationController?.popToRootViewControllerAnimated(true)
or make some hack with navigation stack
var viewCtonrollers = self.navigationController?.viewControllers
let firstViewCtr = viewCtonrollers?.first;
viewCtonrollers?.removeAll()
viewCtonrollers?.insert(firstViewCtr!, atIndex: 0)
self.navigationController?.viewControllers = viewCtonrollers!
Instead of reverting back the navigation, Why not you can set rootViewController for Window again like below?
SharedAppDelegate.window?.rootViewController = MainStoryboard.instantiateViewControllerWithIdentifier("HomeNavigationController")
For Swift 3, use the following code:
self?.navigationController?.popToRootViewController(animated: true)
I have a problem with my UINavigationController when I try to push another controller.
AppDelegate
let testViewController:ProximityQuestionsIntroductionViewController = ProximityQuestionsIntroductionViewController();
self.navController = UINavigationController(rootViewController: testViewController);
self.window = UIWindow(frame: UIScreen.mainScreen().bounds);
self.window!.rootViewController = navController;
self.window!.backgroundColor = UIColor.whiteColor();
self.window!.makeKeyAndVisible();
return true
ProximityQuestionsIntroductionViewController
class ProximityQuestionsIntroductionViewController:UIViewController {
// Controls
var proximityIntroductionView:ProximityQuestionsIntroductionView!;
// Load components when the view is loaded
override func viewDidLoad() {
super.viewDidLoad();
print("nav in viewDidLoad : \(self.navigationController)");
//Hide Nav bar
self.navigationController?.navigationBarHidden = true;
// Set view
self.proximityIntroductionView = ProximityQuestionsIntroductionView(frame: self.view.frame);
self.view = self.proximityIntroductionView;
}
func start(){
print(self);
print("start in controller");
let nextController = ProximityQuestionsProgressViewController();
print("nav in start : \(self.navigationController)");
self.navigationController?.pushViewController(nextController, animated: true);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
I call start from my ProximityQuestionsIntroductionView when a button is clicked.
In the log I see "nav in start : nil" and the controller doesn't change.
For more info :
self is really the ProximityQuestionsIntroductionViewController.
the navigation bar is really hidden
So the navigation controller is ok in viewDidLoad but not in the other function.
Can someone tell me why ?
Thanks!
EDIT : AppDelegate
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("ProximityIntroduction");
self.navController = storyboard.instantiateViewControllerWithIdentifier("NavigationController") as! UINavigationController;
self.navController?.setViewControllers([initialViewController], animated: false)
self.window?.rootViewController = navController;
self.window?.makeKeyAndVisible();
I add navigation Controller to the story Board and change the ID. But I still have a nil value for the UINavigationControllerin the function start
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("ProximityQuestionsIntroductionViewController")
navigationController = UINavigationController.init(rootViewController:initialViewController )
navigationController?.setViewControllers([initialViewController], animated: false)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
Put Story board id for this ProximityQuestionsIntroductionViewController
You must mark the window and navController variables as optional:
var window : UIWindow?
var navController : UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
let testViewController:ProximityQuestionsIntroductionViewController = ProximityQuestionsIntroductionViewController();
self.navController = UINavigationController(rootViewController: testViewController);
if let window = window {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds);
self.window!.rootViewController = navController;
self.window!.backgroundColor = UIColor.whiteColor();
self.window!.makeKeyAndVisible();
}
return true
}
If you created the project from an Xcode template:
Remove the key-value pair for key "Main storyboard file base name" from Info.plist.
Delete the storyboard file Main.storyboard.
The problem came from the view.
Doing this :
// Set view
self.proximityIntroductionView = ProximityQuestionsIntroductionView(frame: self.view.frame);
self.view = self.proximityIntroductionView;
causes the problem. I don't know exactly why but putting all the UIView in the controller directly solved the problem.
I am new programming in Swift but i`m having some problems.
I have a login page (LoginViewController) and when i click the button it heads to another View Controller (MainViewController).
The problem is that in MainViewController i'm using a Slide Menu from here: https://github.com/dekatotoro/SlideMenuControllerSwift
and the code where the slide menu is done is in AppDelegate:
private func createMenuView() {
// create viewController code...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController
let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftViewController") as! LeftViewController
let rightViewController = storyboard.instantiateViewControllerWithIdentifier("RightViewController") as! RightViewController
let nvc: UINavigationController = UINavigationController(rootViewController: mainViewController)
UINavigationBar.appearance().tintColor = UIColor(hex: "689F38")
leftViewController.mainViewController = nvc
let slideMenuController = ExSlideMenuController(mainViewController:nvc, leftMenuViewController: leftViewController, rightMenuViewController: rightViewController)
slideMenuController.automaticallyAdjustsScrollViewInsets = true
self.window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
self.window?.rootViewController = slideMenuController
self.window?.makeKeyAndVisible()
}
and in this application function the createMenuView is called:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.createMenuView()
return true
}
the problem with this is that LoginViewController is the Initial View Controller, and when i leave it like that the MainViewController appears as the Initial View Controller.
How can i do so that the LoginViewController is the initial and when the user press the button in login set to MainViewController with all the menu thing.
Please help me i really don`t know what to do :(
It is because you're replacing root view controller to be slideMenuController in createMenuView method and calling it in didFinishLaunchingWithOptions in app delegate. This code will get executed as soon as the app is launched.
Solution: Instead of doing this, try to get the application delegate instance using UIApplication.sharedApplication().delegate in createMenuView method, define this method in LoginViewController and then call this method after button click in LoginViewController (where ever you wanted to call).
Please call createMenuView() method after button click in login view controller, may be in ViewWillAppear of mainViewController.
Rewrite the method like this inside LoginViewController or where ever you want it to be based on your requirement:
private func createMenuView() {
// create viewController code...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController
let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftViewController") as! LeftViewController
let rightViewController = storyboard.instantiateViewControllerWithIdentifier("RightViewController") as! RightViewController
let nvc: UINavigationController = UINavigationController(rootViewController: mainViewController)
UINavigationBar.appearance().tintColor = UIColor(hex: "689F38")
leftViewController.mainViewController = nvc
let slideMenuController = ExSlideMenuController(mainViewController:nvc, leftMenuViewController: leftViewController, rightMenuViewController: rightViewController)
slideMenuController.automaticallyAdjustsScrollViewInsets = true
let appDelegate = UIApplication.sharedApplication().delegate
appDelegate.window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
appDelegate.window?.rootViewController = slideMenuController
appDelegate.window?.makeKeyAndVisible()
}
Call this method on the #IBAction of the login button in LoginViewController.
I hope this answers you're question.