I'm looking for changing my ViewController from a gameScene with SpriteKit.
I have 3 scenes with 3 different ViewControllers.
When my application starts with the PresentationViewController (see the screenshot 1) my transition code from my GameScene (GameViewController) to the GameOverViewController doesn't work and I can read an error message.
error message :
whose view is not in the window hierarchy!
My code is :
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let settingController: UIViewController = storyboard.instantiateViewControllerWithIdentifier("PresentationViewController") as UIViewController
let vc = self.view?.window?.rootViewController
vc?.presentViewController(settingController, animated: true, completion: nil)
And when my app starts with the GameViewController (see the screenshot 2) the transition code works perfectly.
I don't know why is there a difference between this 2 cases.
For information the transition between PresentationViewController and GameViewController is with a UIButton with this code :
override func viewDidLoad() {
super.viewDidLoad()
var playButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
let image = UIImage(named: "playButton.png") as UIImage
playButton.frame = CGRectMake(0, 0, 100, 100)
playButton.center = CGPointMake(self.view.frame.width/2, self.view.frame.height/1.7)
playButton.addTarget(self, action: "transition:", forControlEvents: UIControlEvents.TouchUpInside)
playButton.setBackgroundImage(image, forState: UIControlState.Normal)
self.view.addSubview(playButton)
}
func transition(sender:UIButton!)
{
println("transition")
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("GameViewController") as UIViewController
secondViewController.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self.presentViewController(secondViewController, animated: true, completion: nil)
}
Well I found by myself a way to navigate between several ViewController.
I set the secondViewController to be the rootViewController in replace this code in my PresentationViewController file func transition():
self.presentViewController(secondViewController, animated: true, completion: nil)`
by this code (with options) :
secondViewController.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
let window = UIApplication.sharedApplication().windows[0] as UIWindow
UIView.transitionFromView(
window.rootViewController!.view,
toView: secondViewController.view,
duration: 0.65,
options: .TransitionCrossDissolve,
completion: {
finished in window.rootViewController = secondViewController
})
Related
From my onboarding viewcontroller i need to transition to my tabBarController which is also a navigationController and i want to make it as a root viewcontroller afterwards.
#objc func willGoToMain(sender: UIButton!) {
let tabBarController = TabBarController()
let navigationController = UINavigationController(rootViewController: tabBarController)
navigationController.isNavigationBarHidden = true
self.present(tabBarController, animated: true, completion: nil)
}
Thread 1: "Application tried to present modally a view controller <MyStarterProject.TabBarController: 0x7f9bd8011400> that has a parent view controller <UINavigationController: 0x7f9bd7022800>
I like to transition it like a modal presentation or cross dissolve. not just to appeared as a rootviewcontroller all of a sudden.
Here's what i'm trying to do and this solves my problem.
#objc func willGoToMain(sender: UIButton!) {
guard let window = UIApplication.shared.keyWindow else {
return
}
let tabbarController = TabBarController()
let navigationController = UINavigationController(rootViewController: tabbarController)
navigationController.isNavigationBarHidden = true
window.rootViewController = navigationController
window.makeKeyAndVisible()
let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.3
UIView.transition(with: window, duration: duration, options: options, animations: {}, completion:
{ completed in
// maybe do something on completion here
})
}
Change this:
self.present(tabBarController, animated: true, completion: nil)
with this:
self.present(navigationController, animated: true, completion: nil)
Please try this one.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainTabBarController = storyboard.instantiateViewController(identifier: "MainTabBarController")
mainTabBarController.modalPresentationStyle = .fullScreen
self.present(mainTabBarController, animated: true, completion: nil)
If you need more details. please visit this blog
https://fluffy.es/how-to-transition-from-login-screen-to-tab-bar-controller/
I have a question regarding blureffects on a view controller. I program all of my segues manually for several reasons. But when I go to a viewcontroller that has a blureffect background, I only see a dark gray background. How can I solve this that the view controller blur effect lays on top of another viewcontroller where the content of the previous viewcontroller can be seen through?
The blureffects are now applied by storyboard with transparant background views.
My code for seque is:
Action:
#IBAction func ToUserSections(_ sender: Any) {
let controller =
Navigation.getDestinationViewControllerWith("User",
controllerName: "UserSectionsVC")
present(controller, animated: false, completion: nil)
}
Class:
class Navigation{
static func getDestinationViewControllerWith(_
storyboardName:String, controllerName:String)-> UIViewController{
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let controller =
storyboard.instantiateViewController(withIdentifier:
controllerName) as UIViewController
return controller
}
}
Hope you can help me with this because it will maximize aesthetics for the app :D
try this.
let viewController : CustomViewControlller = UIStoryboard.getMainStoryboard().instantiateViewController(withIdentifier: "Identifier") as! CustomViewControlller
viewController.modalPresentationStyle = .overCurrentContext
viewController.modalTransitionStyle = .crossDissolve
self.present(viewController, animated: true, completion: { _ in })
Alright for others to know, what worked for me was the following:
At IBAction: adding the following line before presenting
controller.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
Like this:
#IBAction func ToUserSections(_ sender: Any) {
let controller = Navigation.getDestinationViewControllerWith("User", controllerName: "UserSectionsVC")
controller.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
present(controller, animated: false, completion: nil)
}
My goal is to present a UIViewController as a popover programmatically.
As you can see , the transition style is set to Cross Dissolve and the presentation is set to Over current context. So technically if I click the button, the transition will work, my goal is to do it programmatically.
func clickButton() {
//What should I do here?
}
How do I present the popover programmatically on clickButton?
If you are presenting view controller modally. You can do like this.
func clickButton() {
if let vc = self.storyboard?.instantiateViewController(withIdentifier:"YOUR_VIEW_CONTROLLER_ID") {
vc.modalTransitionStyle = .crossDissolve;
vc.modalPresentationStyle = .overCurrentContext
self.present(vc, animated: true, completion: nil)
}
}
You can try this:
let rateViewController = RatePopupVC()
rateViewController.modalTransitionStyle = .crossDissolve
rateViewController.modalPresentationStyle = .overCurrentContext
self.present(rateViewController, animated: true, completion: nil)
If you want to close it programmatically
Inside PopupVC
self.dismiss(animated: true, completion: nil)
Try this :
func clickButton() {
let vc : PopupVC = storyboard!.instantiateViewControllerWithIdentifier("PopupVCID") as! PopupVC
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.presentViewController(vc, animated: true, completion: nil)
}
Try following code.
let popupview = storyboard?.instantiateViewController(withIdentifier: "YourViewController")
popupview.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
self.addChildViewController(popupview)
self.view.addSubview(popupview.view)
popupview.didMove(toParentViewController: popupview)
I am facing a problem, because there are two ways how do I display my ViewController.
First way is performSegue(withIdentifier: "segue", sender: self)
It works fine because then I have this back button in my navigationItem:
Then I use this code to present the same ViewController (from another viewController than in the first case):
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navVC = storyboard.instantiateViewController(withIdentifier: "navViewController") as! UINavigationController
let vc = navVC.topViewController as! ViewController
self.present(navVC, animated: true, completion: nil)
but then I do not have any back button in my ViewController.
My question is: How can I keep my backButton (exactly how it is) when I use this function: performSegue(withIdentifier: "segue", sender: self) , but add button (can look different) when I use this function: self.present(navVC, animated: true, completion: nil)
Note: In my case 1 my segue is connected right to ViewController , but in case 2 I present UINavigationController and ViewController is embed in in this UINavigationController.
Edit: I tried this code, but it always prints: "1.........":
if self.presentingViewController != nil {
print("1..........")
} else if self.navigationController?.presentingViewController?.presentedViewController == self.navigationController {
return print("2.........")
} else if self.tabBarController?.presentingViewController is UITabBarController {
return print("3........")
}
And also this code prints:"Else.............." :
let isPresentingInAddMealMode = presentedViewController is UINavigationController
if isPresentingInAddMealMode {
print("FirstOption......................")
} else {
print("Else......................")
}
If you need more info just let me know.
Thank you very much.
you need to check presentedViewController and add back button as below .
if ([self presentedViewController]) {
// add your back button item here
}
Try this:
let viewController = storyboard?.instantiateViewController(withIdentifier: "Login") as! LoginView
let customNavigation = UINavigationController(rootViewController: viewController)
customNavigation.navigationBar.tintColor = UIColor.black
self.present(customNavigation, animated: true, completion: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Login"
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(self.backButton))
}
func backButton() {
self.dismiss(animated: true, completion: nil)
}
Try to set Image in UIBarButtonItem
I have solved it!! I put restorationIdentifier to my root navigationController and then I just check if that is navigationController with my restorationIdentifier like this:
if self.navigationController?.restorationIdentifier == "navViewController"{
let leftItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(goBack))
self.navigationItem.leftBarButtonItem = leftItem //Adds item if id is navViewController
}else{
self.navigationItem.leftBarButtonItem = nil //removes it and keeps "<Back"
}
After i set my initial ViewController manually in the AppDelegate.swift
my UISwipeGestureRecognizer is not working.
This is how my AppDelegate look like:
self.mainNavigationController = UINavigationController()
var mainController: UIViewController? = TineLineViewController()
self.mainNavigationController!.pushViewController(mainController!, animated: true)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.rootViewController = mainController
self.window!.makeKeyAndVisible()
and this is how my UISwipeGestureRecognizer is set in the initial ViewController:
let postLeft : Selector = "postLeft:"
let postLeftSwipe = UISwipeGestureRecognizer(target: self, action: postLeft)
postLeftSwipe.direction = UISwipeGestureRecognizerDirection.Left
goToPostButton.addGestureRecognizer(postLeftSwipe)
And this is my postLeft method which instantiate a new ViewController and navigate to it.
func postLeft(sender: AnyObject) {
println("left swipe to push...")
let secondViewController = PostCreateController()
self.presentViewController(secondViewController, animated: true, completion: nil)
}
When I swipe, the postLeft method was called, I can see the println left swipe to push, but the application doesn't changed the view.
Does anybody have any solution to solve this issue?
Thank you!
UPDATE:
i change my postLeft method. and by the console i see my new viewController class instantiates but it's not showing up.
I think you don't set your navigationController correctly,
try this instead of self.navigationController!.pushViewController
let secondViewController = PostCreateController()
self.presentViewController(secondViewController, animated: true, completion: nil)