Custom Animations while changing RootViewcontroller - ios

on the very first launch of my app i wanna show a Specific View Controller say firstLaunchVC and for that i'm doing this
in my didFinishLaunchingWithOptions
if (itsFirstLaunch){
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var initialViewController: UIViewController
initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("SignupViewController") as! SignupViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
its working fine but now after completing my signup i want to change my RootViewController to my default ViewController say DefaultVC
appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var initialViewController: UIViewController
initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("DefaultVC") as! UITabBarController
UIView.transitionWithView(self.appDelegate.window!, duration: 0.3, options: .TransitionFlipFromBottom, animations: {() -> Void in
self.appDelegate.window?.rootViewController = initialViewController
self.appDelegate.window?.makeKeyAndVisible()
}, completion: { _ in }
)
its working fine and you can also see that i added animation in this Transition
UIView.transitionWithView(self.appDelegate.window!, duration: 0.3, options: .TransitionFlipFromBottom, animations: {()....
but this animation is not good enough also here we have a lot of options like :
TransitionFlipFromLeft
TransitionFlipFromRight
TransitionCurlUp
TransitionCurlDown
TransitionCrossDissolve
TransitionFlipFromTop
TransitionFlipFromBottom etc...
but none of them are meeting my requirements (the are totally hideous) what i want is the Animation when a ViewController being dismissed i want to slide out the vc from the frame in a direction if anybody can guide me how to do this then it'll be so helpful for me

Related

How to invoke Navigation controller form a VC

I have 3 VC's (VC as in ViewController)
1 AuthViewController which is the entry point for the app and it checks if user is logged in else it displays the sign-in related views
2 HomeViewController is the main interface of the app landing page after the user has logged in
3 ListViewController which displays a list, invoked via a segue on HomeViewController.
I want to enable back navigation between ListViewController and HomeViewController via a Navigation Controller.
How can I achieve this If I have a Navigation controller whose root VC is HomeVC. how can I invoke it from my AuthVC so that it gets invoked with the Navigation controller.
I have tried to invoke the Navigation controller but did not work
I have also tried invoking the HomeVC by
let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "mainViewController") as? UIViewController {
viewController.modalPresentationStyle = .overCurrentContext
self.present(viewController, animated: true, completion: nil)
although I was able to invoke the HomeVC could not get the navigation buttons
How can I invoke HomeVC from the AuthVC without loosing the NavigationController
You need 1 navigation controller, not 3.
If you want to push a new controller, then use push method, not present.
The back button gets enabled automatically on the navigation bar if there are controllers in the navigation stack and the current controller is not a root view controller.
When coming from AuthViewController to HomeController, you should change the RootViewController. So that the user cant go back to the Auth screen. That would make no sense.
Once you are on HomeController, you can push other controller using the below code and it will add a back button to your controller automatically.
let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "mainViewController") as? UIViewController {
self.navigationController?.pushViewController(vc, animated: true)
}
You can also use the below extension:
extension UIViewController {
func pushVC(storyboardName : String, vcname : String) {
let vc = UIStoryboard.init(name: storyboardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcname)
vc.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(vc, animated: true)
}
func popVC() {
self.navigationController?.popViewController(animated: true)
}
func makeRootVC(storyBoardName : String, vcName : String) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let vc = UIStoryboard(name: storyBoardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
let nav = UINavigationController(rootViewController: vc)
nav.navigationBar.isHidden = true
appDelegate.window?.rootViewController = nav // If using XCode 11 and above, copy var window : UIWindow? in your appDelegate file
let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.6
UIView.transition(with: appDelegate.window!, duration: duration, options: options, animations: {}, completion: nil)
}
}

Transition Animation is not working Swift 5

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)

Getting warning "Presenting view controllers on detached view controllers is discouraged" while making window root view controller in app delegate?

Here is the code
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "homeTBC") as! UITabBarController
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
cant figure it yet.
I have tried this code in My App for root view controller in app delegate working perfect:
As I think you are using this code in presented view controller or in presented navigation controller: Please send the scenario(Screen Shot) exact what you are doing?
var window: UIWindow?
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "TabbarVC") as! TabbarVC
self.window?.rootViewController = vc
From Apple's Documentation
When creating windows, always set the window’s initial size and
specify the screen on which it is displayed.
Add the size:
self.window = UIWindow.init(frame: UIScreen.main.bounds)
Your complete code should be like that:
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "homeTBC") as! UITabBarController
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()

Present New ViewController programmatically [Swift 3]

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

Access UINavigation Controller from the app delegate

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

Resources