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)
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'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 !!
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
What I am trying to do is present a new view controller programmatically with Swift 3 with an animated fade-out then fade-in and a pop-up then pop-down depending on what view controllers the user enters. This is so my app feels more modern and less old and blocky.
Here is the current method I am using for presenting a new view controller. It works, but it is rather abrupt and robotic:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let ExampleVC = storyBoard.instantiateViewController(withIdentifier: "ExampleVC")
//ExampleVC standing for ExampleViewController
self.navigationController?.pushViewController(ExampleVC, animated: false)
here is a method for animating UIViews but it doesnt work with UIViewControllers
func popIn(yourView : UIView) {
yourView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
UIView.animateKeyframes(withDuration: 0.2, delay: 0.0, options: UIViewKeyframeAnimationOptions.calculationModeDiscrete, animations: {
yourView.transform = .identity
}, completion: nil)
self.view.addSubview(yourView)
}
i need something similar to this but for a UIViewController
UPDATE
here is what you would use to to present a new viewcontroller in a cool way to make your app more modern
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let VC = storyboard.instantiateViewController(withIdentifier: "Example") //<<< make sure you enter in your storyboard ID here or you will crash your app
VC.modalTransitionStyle = .crossDissolve //you can change this to do different animations
VC.view.layer.speed = 0.1 //adjust this to animate at different speeds
self.navigationController?.present(VC, animated: true, completion: nil)
if you have any questions comment them so i can help you guys out
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var ivc = storyboard.instantiateViewController(withIdentifier: "ExampleVC")
ivc.modalTransitionStyle = .crossDissolve
ivc.view.layer.speed = 0.1
self.present(ivc, animated: true, completion: { _ in })
Check this code for animation to view controller
How to i segue over to another UINavigationViewcController programmatically so there is a back button to go to the previous screen. I tried using this code, but it just models over.
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("NavViewController") as FirstMemberViewController
self.presentViewController(resultViewController, animated:true, completion:nil)
Try this
// Your controllers
let navigationController = UINavigationController()
let viewController = UIViewController()
// The push
navigationController.pushViewController(viewController, animated: true)
I found the answer...
navigationController!.pushViewController(storyboard!.instantiateViewControllerWithIdentifier("Home") as! UIViewController, animated: true)