I want to add a transition effect to my app from a ViewController to another.
I want use the transition effect that the Navigation Controller usually add (sliding effect) but without using the Navigation Controller.
What is the simplest and easiest way to achieve that? (I'm using swift 3)
EDIT:
My Code Now:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func GoToMenu(_ sender: UIButton) {
//TRANSITION EFFECT
let nextVCID="010101"
guard let presentedController = self.storyboard?.instantiateViewController(withIdentifier: nextVCID) else { return }
presentedController.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.present(presentedController, animated: true, completion: nil)
}
}
You can present it modaly and set modal transition style you like.
guard let presentedController = self.storyboard?.instantiateViewController(withIdentifier: nextVCID) else { return }
presentedController.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.present(presentedController, animated: true, completion: nil)
Related
I'm using NavigationController to operate the view.
If the value is exceeded on the first view and the value is not satisfied with the 'if' on the second view, I hope 'dismiss' will work.
I have made a similar simple example.
//First View
class ViewController: UIViewController {
#IBAction func goSecond(_ sender: Any) {
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let DvC = Storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(DvC, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
//Second View
class SecondViewController: UIViewController {
#IBAction func goThird(_ sender: Any) {
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let DvC = Storyboard.instantiateViewController(withIdentifier: "ThirdViewController") as! ThirdViewController
let num = 1
DvC.num = num
self.navigationController?.pushViewController(DvC, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
//Third View
class ThirdViewController: UIViewController {
var num = Int()
override func viewDidLoad() {
super.viewDidLoad()
print("num: ")
print(num)
if(num != 2) {
print("Success")
// dismiss(animated: true, completion: nil)
// navigationController?.dismiss(animated: true, completion: nil)
} else {
print("Failed")
}
}
}
The third view shows "Success", but the codes below it do not work.
dismiss(animated: true, completion: nil) // not work
navigationController?.dismiss(animated: true, completion: nil) // not work
Pressing the button on the first view moves to the second view, and pressing the button on the second view moves to the third view.
If the value passed when I go to the third view is not satisfied with 'if', I want to go back to the first view.
I want the second and third views to end, not just the screen shift.
Like the finish() of Android.
How can I exit the second and third views and return to the first view?
You are pushing the view controllers onto the stack. dismiss is for dismissing VCs presented modally.
What you want to do is pop the VC to go back one.
navigationController?.popViewController(animated: true)
or to go back to the root view.
navigationController?.popToRootViewController(animated: true)
I am trying to use same View Controller for showing my second view which should be inside Second View Controller. The motive is to reuse the same View Controller for different View while not losing the push animation effect.
See my code below:-
class PictureCaptureVC: UIViewController {
public var isReviewPhoto = false
override func viewDidLoad() {
super.viewDidLoad()
if isReviewPhoto {
btnSubmit.isHidden = false
} else{
btnSubmit.isHidden = true
}
}
#IBAction private func handleReviewBtn(_ sender: UIButton) {
if let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "PictureCaptureVC") as? PictureCaptureVC {
secondViewController.isReviewPhoto = true
self.present(secondViewController, animated: true, completion: nil)
self.dismiss(animated: false, completion: nil)
}
}
}
It is creating a UITransitionView Over the Top View and i am even concerned about memory leaks.
See This Image
Any Help would be highly appreciated!
How to show image in overCurrentContext view in the ViewController2.swift class ? I am doing this correctly when i worked manually but not doing when i using this programmatically in swift.
ViewController1.swift code -
#IBAction func btnImage(_ sender: Any)
{
let imageVC = self.storyboard?.instantiateViewController(withIdentifier: "ImageVC") as! ImageVC
self.navigationController?.pushViewController(imageVC, animated: true)
}
ViewController2.swift code -
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.modalPresentationStyle = .overCurrentContext
}
I want this background alpha 0.7 and using .overCurrentContext
Output -
You are pushing your viewController, Instead you should present it with modalPresentationStyle .overCurrentContext. Do something like this:
#IBAction func btnImage(_ sender: Any)
{
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ImageVC") as! ImageVC
vc?.view.backgroundColor = UIColor.black.withAlphaComponent(0.7)
vc?.modalPresentationStyle = .overCurrentContext
vc?.navigationController?.isNavigationBarHidden = true
self.navigationController?.present(vc, animated: true, completion: nil)
}
For more information you can read more about modalPresentationStyle in Apple Docs.
Hope it helps!!
I want presented view controller vc2 in smaller size than the screen, how to do that in Swift 3 ?? Thanks for help.
This is my code:
#IBAction func leftButtonPressed(_ sender: UIButton) {
let vc2 = self.storyboard?.instantiateViewController(withIdentifier: "Controller2") as! ViewController2
vc2.modalPresentationStyle = .currentContext
present(vc2, animated: true, completion: nil)
}
Try this..
#IBAction func leftButtonPressed(_ sender: UIButton) {
let vc2 = self.storyboard?.instantiateViewController(withIdentifier: "Controller2") as! ViewController
vc2.providesPresentationContextTransitionStyle = true
vc2.definesPresentationContext = true
vc2.modalPresentationStyle=UIModalPresentationStyle.overCurrentContext
self.present(vc2, animated: true, completion: nil)
// Make sure your vc2 background color is transparent
vc2.view.backgroundColor = UIColor.clear
}
You can simply use a container view to display the smaller view controller. Here is a great tutorial on container views: https://cocoacasts.com/managing-view-controllers-with-container-view-controllers/
Hope this was helpful :)
As SplitViewController loads, I am showing a Login Screen. On successful login, I need to go back to parent view controller. Somehow dismissal is not working for me. Here is the code:
ParentViewController:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if !appDelegate.loggedIn {
self.performSegueWithIdentifier("loginScreen", sender: self)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
Child ViewController:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.loggedIn = true
self.dismissViewControllerAnimated(true, completion: nil)
The dismissal part never works. It just hangs on Login Screen.
Try one of the following:
1) remove self. keep only dismissViewControllerAnimated(true, completion: nil)
or remove self. and make it:
2) presentingViewController.dismissViewControllerAnimated(true, completion: nil)
or remove self. and try:
3) presentedViewController.dismissViewControllerAnimated(true, completion: nil)
Try this in your parent view controller:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if !appDelegate.loggedIn {
let loginVC: UIViewController = self.storyboard!.instantiateViewControllerWithIdentifier("LoginViewController") as UIViewController
loginVC = UIModalTransitionStyle.CoverVertical
self.parentViewController?.presentViewController(loginVC, animated: true, completion: nil)
}
}
You're instantiating the new view controller by its own name rather than by the segue name.