I'm pretty new to swift development but I am trying to implement a back button in my application.
I have the following layout in my Main.storyboard
As seen in the picture the navigation bars display however when i lunch my app they are not visible.
I also have tried using a button that will take me back to the previous view with
self.navigationController?.popViewControllerAnimated(true)
but it has not worked
Update:
The image above doesnt show the initial controller which is my ViewController
the problem is that All Routes is actually a PageContentViewController which is called from ViewController which checks for fb login than calls:
dispatch_async(dispatch_get_main_queue()){
self.setViewControllers([self.getViewControllerAtIndex(0)] as [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}
So i cannot change my initial view controller.
I dont know how I can get around this problem.
Is there a way to do it from Main.storyboard or a programistic solution ?
Embedding my initial view in a navigational controller and setting it to the initial view has worked.
Even though it doesnt look right on the preview because my initial view calls the other views programmatically it works correctly on the device
Related
My app starts off with a Tab Bar Controller on the first screen after logging in:
Later on the user can tap to get back to that screen, by pressing the Home button. The code for that is:
self.present(self.mealPlanViewController, animated: true, completion: nil)
The issue is that when they go back to that view controller, the Tab Bar is no longer there.
How can I present the view controller with the Tab Bar again?
You said:
"Later on the user can tap to get back to that screen, by pressing the Home button. The code for that is:"
self.present(self.mealPlanViewController, animated: true, completion: nil)
That is wrong. You are not "getting back" to anything. You are presenting self.mealPlanViewController modally. That means it is being drawn on top over everything else. If self.mealPlanViewController was previously on-screen, bad things may happen.
In order to help you sort it out, you need to explain your view controller hierarchy and the flow between screens. What is the name of your tab bar controller's class? Of the view controllers that are displayed as tabs? How are you getting from the tab bar controller to the screen that contains the home button?
I was able to figure it out - please see below:
I added #IBAction func unwindToContainerVC(segue: UIStoryboardSegue) {} to the view controller I wanted to go back to. This added an Action Segue.
I added the segue to the storyboard and gave it the identifier, "unwindToMealPlanViewController"
I added the following code to my Done button: self.performSegue(withIdentifier: "unwindToMealPlanViewController", sender: nil)
These were some very helpful resources in solving this:
How to perform Unwind segue programmatically?
https://www.hackingwithswift.com/example-code/uikit/how-to-perform-a-segue-programmatically-using-performsegue
https://www.youtube.com/watch?v=ULd2v4mHyQ4
I've had this issue for months with multiple views, both Apple provided like ImagePicker and VCs from storyboard.
I believe that it has something to do with the underlying views we have both a tab bar controller and navigation controller in most views.
Strange thing is using some open source views from pods does not cause this bug.
So I'm two views deep on a navigation controller and present another view modally on top with present(vc, animated: true, completion: {})
Works like a charm, now dismissing that view with dismiss(animated: true, completion: nil) throws me back all the way to the initial view or root view of the navigation controller, had both happen before, depending on the presented view.
Update:
Build a sample project trying to reproduce the behavior but failed. Drew a reduced diagram to better explain the current bug behavior.
Also noticed that if I'm invoking the post view one step earlier in the Fandom view it works as expected.
In my case i am using UITabBarController, and I wrote code in viewWillAppear of UITabBarController
self.selectedIndex = 2
so when i present any thing from any controller whose parent is UITabBarController and when i dismiss that it automatically open third tab of UITabBarController.
Maybe you explicitly wrote any code to select specific index of TabBar.
Maybe this is useful for you or anyone else.
I'm having an issue with my main.storyboard file. I changed the settings of my app so that the start screen is the main.storyboard file, rather than LaunchScreen.xib. The initial ViewController is the NavigationController, and the second is my SplashScreeViewController. (I created my own splash screen in the storyboard so that I could change it with additional code.) I use a line of code in my splash screen to later transition to the second view controller. Here it is:
var controller:UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("Second") as! ViewController
controller.modalTransitionStyle = .CrossDissolve
self.presentViewController(controller, animated: true, completion: nil)
For some reason, when I transition to that second ViewController, the navigation bar that should be at the top (of the second ViewController) isn't there, opposite of what was shown in the main.storyboard file. I tried to add an invisible, disabled button to the splash screen as to add a connection between the two view controllers, and therefore adding a navigation bar to the second, but when the splash transitions on its own, no navigation bar appears on the second.
Is there a way I could have a navigation bar on my second view controller without dragging one in, nor programming it in? I would like to use the one Xcode provides when you create a new connection between controllers.
Thanks in advance to all who reply.
*(I apologize for the lack of pictures to help describe my problem. I don't have enough 'reputation' to do so.)
You need to embed your Second view controller on a Navigation Controller, and then instantiate that navigation controller instead.
This might solve the problem - It looks like you are presenting a modal view controller when you probably want to be pushing your view controller from your initial navigation controller with pushViewController
I am developing an App that has the following set up:
There is a login screen; if the login is successful, then the tab bar view is opened. All views are created in main.storyboard. The opening is handled as follows:
instantiateViewControllerWithIdentifier("personalViewController") as! PersonalViewController
self.presentViewController(vc, animated: true, completion: nil)
It does open my new view controller, but the bar items are not visible. Anyone knows how to fix this?
Thanks!
You have to open the TabBarViewController instead of the viewcontroller that is inside of the tabbar viewcontroller.
I recommend just looking at the documentation in XCode. All documentation is written in Swift and Objective C so it is very easy to translate between the two languages. Also read apple's swift basics to understand this code translation better: https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_467
presentViewController is going to open the personalViewController as a modal view. If you want to push the view onto the navigation controller stack (i.e. open it like a new screen) then you could/should use:
self.navigationController?.pushViewController(viewController: UIViewController, animated: Bool)
I have an application that links to a view via a button (which is embedded inside a navigation controller)
Which all works fine! I hit the button and the view is presented using Segue Show Eg: Push. The view pops over and I see a UINavigationBar with my title and a button to dismiss.
My question is.. I need to programatically call this view again in my code, however, when I do this the view appears without any UINavigation. I assume it does this because it's no no longer embedded inside the UINavigationController?
Whats the best solution for this? Should I programatically create the UINavigationBarController? If so how would this look in swift?
Thank in advanced.
// Code below is triggered from an action (button) on UIAlertController.
// This loads up the "Scan Barcode view" without the UINavigationBar embedded.
let scannerViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ScannerViewController") as ScannerViewController
self.presentViewController(scannerViewController, animated: true, completion: nil)
When I set up the connection using interface builder from another button (located elsewhere) so show the same view. It works perfectly showing the "Scan Barcode" embedded inside the Navigation Controller.
When using interface builder to link (from the camera button) it works perfect.
Thanks to #kpsharp the solution was just to use
self.navigationController?.pushViewController(scannerViewController, animated: true)