I'm a bit beginner in SWIFT and right now I'm facing a problem whit UI. In this PHOTO I'm showing my UI to clarify what I'm saying . in part 1 I check if the user is logged in to his account or not, if yes it goes to part 3, if not it goes to part 2. when user login in part 2, I transfer the user to part 3.
part 1 and 2 should not have any navigation color, though the part 3 should have the navigation color.
Part 1:
if let token = UserDefaults.standard.string(forKey: ConstantsKey.token){
if !token.isEmpty{
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
let rootController = UINavigationController(rootViewController: vc)
self.present(rootController, animated: true, completion: nil)
}else{
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "LoginVc")
let rootController = UINavigationController(rootViewController: vc)
self.present(rootController, animated: true, completion: nil)
}
}else{
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "LoginVc")
let rootController = UINavigationController(rootViewController: vc)
self.present(rootController, animated: true, completion: nil)
}
Part 2:
let storyboard : UIStoryboard = UIStoryboard(name: "MainTabBar", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
let rootController = UINavigationController(rootViewController: vc)
self.present(rootController, animated: true, completion: nil)
I want to have that red color in part 3 ! but whenever I run the application in shows the defualt color of the navigation controller
does anybody knows how should I manage/handle this problem?
Then in Part 1:
if !token.isEmpty{
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
let rootController = UINavigationController(rootViewController: vc)
rootController?.navigationBar.barTintColor = UIColor.red
self.present(rootController, animated: true, completion: nil)
Part 2:
let storyboard : UIStoryboard = UIStoryboard(name: "MainTabBar", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
let rootController = UINavigationController(rootViewController: vc)
rootController?.navigationBar.barTintColor = UIColor.red
self.present(rootController, animated: true, completion: nil)
While logging in set
UserDefaults.standard.set(true,forKey: ConstantsKey.token)
And in routing page check this
if UserDefault.standar.bool(forKey: ConstantsKey.token){
//Go to home page
}else {
setUpNav()
//Go to login page
}
setting navigation
this one will set for whole app
func setUpNav(){
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().tintColor = UIColor.red
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: UIFont(name:"Cairo-Bold", size:22.0)!]
}
if you want for single VC,
write this inside that VC,
make sure that VC's root is navigationController
self.navigationController.navigationBar.barTintColor = .white
self.navigationController.navigationBar.tintColor = .red
Even if you haven't set any value, it doesn't crash !!
Related
From a button action, I have presented a viewController, from that presented VC, add another button and on that button action added a childView like this -
let vc = UIStoryboard(name: "Profile", bundle: nil).instantiateViewController(withIdentifier: "APPopUpViewControllerViewController") as! APPopUpViewControllerViewController
self.addChild(vc)
vc.view.frame = self.view.frame
self.view.addSubview(vc.view)
vc.didMove(toParent: self)
All are working perfectly till now, but when i try to push a viewController from this child VC , it does not work. How can i push a viewController from a childView ??
Push viewController code -
let storyboard = UIStoryboard(name: "WPLogin", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "WPSigninViewController") as! WPSigninViewController
self.navigationController?.pushViewController(vc, animated: true)
My guess is that self.navigationController is nil, so your optional chaining is failing.
You can try
print(parent)
print(parent?.navigationController)
self.parent?.navigationController?.pushViewController(vc, animated: true)
How do you present your ViewController ? If You have pushed it to a navigation controller, then your code is perfect and it should work I think.
If not, then try to present the WPSigninViewController modally as
let storyboard = UIStoryboard(name: "WPLogin", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "WPSigninViewController") as! WPSigninViewController
self.present(vc, animated: true)
and It should work.
If ViewController is your rootController, you can add a NavigationController to it :
let viewController = ViewController(nibName: nil, bundle: nil)
let navigationController = UINavigationController(rootViewController: viewController)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
I am trying to navigate from one view controller to other using tap gesture navigation. I want add transition animation into it. I tried with two ways but it didn't worked for me. -
First approach :
UIView.animate(withDuration: 0.5) { ()-> Void in
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let myTabBarController = storyBoard.instantiateViewController(withIdentifier: "profileViewController") as! ProfileViewController
var appDel = UIApplication.shared.delegate as! AppDelegate
appDel.window?.rootViewController = myTabBarController
self.view.layoutIfNeeded()
}
Second approach :
UIView.animate(withDuration: 0.25,
delay: 0.0,
options: [.curveEaseIn],
animations: {
let controller = self.storyboard!.instantiateViewController(withIdentifier: "mainMenuViewController") as! MainMenuViewController
self.addChild(controller)
self.view.addSubview(controller.view)
controller.didMove(toParent: self)
}, completion: nil)
Can anybody please tell me whats the solution ?
try this to present
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "profileViewController") as! ProfileViewController
self.present(nextViewController, animated:true, completion:nil)
If you present a ViewController then you can only Dismiss it, It can be done by below code
self.dismiss(animated: true, completion: nil)
To push
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "profileViewController") as! ProfileViewController
self.navigationController?.pushViewController(nextViewController, animated:true)
If you push a ViewController then you can only Pop it, It can be done by below code
self.navigationController?.popViewController(animated: true)
I have a login page a the beginning of my app. when user is granted I'll redirect it to another storyboard.
In this below photo1: the white screen check is user granted or not. if yes I'll redirect the to Photo2. else I'll redirect them to the login page ( red pages in photo1).)
In below Photo2:(I'll show a table view which contains some data. when the user clicks one of them, it goes to the next page (right one).)
And Photo3 is just to clarify Photo2.
The problem is Photo2 after a user clicked a row in the table view. the back button does not work (it is visible in the app)
The code below shows the white screen's code in Photo1:
if let token = UserDefaults.standard.string(forKey: ConstantsKey.token){
if !token.isEmpty{
let storyboard : UIStoryboard = UIStoryboard(name: "MainTabBar", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
let rootController = UINavigationController(rootViewController: vc)
rootController.navigationBar.barTintColor = UIColor.init(red: 229/255, green: 28/255, blue: 60/255, alpha: 1)
self.present(rootController, animated: true, completion: nil)
}else{
// let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "LoginVc")
self.present(vc, animated: true, completion: nil)
}
}else{
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "LoginVc")
self.present(vc, animated: true, completion: nil)
}
The code below shows the login page after the user is granted:
let storyboard : UIStoryboard = UIStoryboard(name: "MainTabBar", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
let rootController = UINavigationController(rootViewController: vc)
self.present(rootController, animated: true, completion: nil)
The code below shows the way I redirect the user to the page which has the problem in the photo 2:
let next = self.storyboard?.instantiateViewController(withIdentifier: "ShopVc") as! ShopViewController
self.navigationController?.pushViewController(next, animated: true)
I Also have added the code below to Delegate :
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
UINavigationBar.appearance().shadowImage = UIImage()
Am I doing something wrong here which might cause this problem?
////////////////////////////////////////////////////////////////
ANSEWER
I created a new project which works fine there! I think it was the xCode's problem!
restart your project , this could be an Xcode problem
From appDelegate I want to access a view controller which is embedded with UINavigation controller.
How can I do so, knowing that I can directly access that view controller but in this case the navigation bar will not appear, therefore this is not what I want.
I have tried the code found below but it prompts a warning and a black screen is presented.
var storyboard1 = UIStoryboard(name: "Main", bundle: nil)
var viewController: ExamNav_ViewController = storyboard1.instantiateViewControllerWithIdentifier("ExamNav_ViewController") as! ExamNav_ViewController
var rootViewController = self.window!.rootViewController as! UINavigationController
// rootViewController.pushViewController(viewController, animated: true)
rootViewController.presentViewController(viewController, animated: true, completion: nil)
The warning is: Attempt to present PlayMyWay.ExamNav_ViewController: 0x14fd19b50 on UINavigationController: 0x14fd13a90 whose view is not in the window hierarchy!
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let navController: UINavigationController = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("provaNavController") as UINavigationController
window?.rootViewController = navController
window?.makeKeyAndVisible()
More Click here to learn about that
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var objOrderVC = storyboard.instantiateViewControllerWithIdentifier("ExamNav_ViewController") as! ExamNav_ViewController
let navig = UINavigationController(rootViewController: objOrderVC)
navig.setNavigationBarHidden(false, animated: false)
window?.rootViewController = navig
I am currently showing a viewController in my new storyboard below:
var storyboard : UIStoryboard = UIStoryboard(name: AccountStoryboard, bundle: nil)
var vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
vc.teststring = "hello"
self.presentViewController(vc, animated: true, completion: nil)
However, this presents the viewcontroller without its embedded navigation controller.
I tried changing "WelcomeID" to the navigation controller within the storyboard - however to no success.
I got this working in Objective -C, however don't know how to transform into swift:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SetupStoryboard" bundle:nil];
UINavigationController *navigationController1 = [storyboard instantiateInitialViewController];
navigationController1.modalPresentationStyle = UIModalPresentationFormSheet;
navigationController1.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
WelcomeViewController *vc = (WelcomeViewController *)navigationController1.viewControllers[0];
vc.teststring = #"Hello";
[self presentViewController:navigationController1 animated:YES completion:nil];
How can you do this in swift?
You're definitely on the right track. Unfortunately when you reference a view controller by its storyboard ID it will ignore the fact it is embedded within anything. Same goes for segues when you segue to something embedded, the destination view controller will be the embedding controller, not the controller you're usually interested in. Anyhow, you should be able to fix the problem in a similar way you've done in Objective-C, so this is just an exercise in syntax porting.
Edit: Define storyboard name with string now
let storyboard : UIStoryboard = UIStoryboard(name: "AccountStoryboard", bundle: nil)
let vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
vc.teststring = "hello"
let navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil)
OR you can give your embedding view controller an ID and instantiate that instead.
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("WelcomeID") as SecondViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
Class Name is : SecondCiewController
The answer given by #Chris is works good in older version of swift.
Update Swift 3 & Swift 4
let storyboard : UIStoryboard = UIStoryboard(name: "AccountStoryboard", bundle: nil)
let vc : WelcomeViewController = storyboard.instantiateViewController(withIdentifier: "WelcomeID") as! WelcomeViewController
vc.teststring = "hello"
let navigationController = UINavigationController(rootViewController: vc)
self.present(navigationController, animated: true, completion: nil)
Thanks!!!
Remember to setup the modalPresentationStyle property, in programmatically present the IB settings are ignored.
let sb = UIStoryboard(name: "retail_carrello", bundle: nil)
guard let carrelloVC = sb.instantiateViewController(withIdentifier: "mainCarrello") as? retail_carrello else { return }
let navController = UINavigationController(rootViewController: carrelloVC)
navController.modalPresentationStyle = .fullScreen //<--- remember to set up this if you need fullscreen
present(navController, animated: true, completion: nil)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SalesVC") as! SalesVC
navigationController?.pushViewController(vc, animated: true)