iOS Storyboard Back Button

iOS Storyboard Back Button

Here is what you need to do:
CustomBack_XCODE

Result:

CustomBack_SIMULATOR

Important: you should specify Back Button text not for the VC where you see back button but for the VC that is the "parent" of the desired screen.

Back button in a referenced storyboard

Follow these steps to achieve, that you want:

  1. In your main (first) storyboard, connect a button with a storyboard reference using push segue.

Sample Image


  1. Storyboard Reference: Select storyboard reference of second (other) storyboard and assign any value to reference id like 'SecondStoryboardVC'

Sample Image


  1. In your second storyboard view controller, assign the same value (like SecondStoryboardVC) to viewcontroller's storyboard id.

Sample Image

Here is result:

Sample Image

Back button to another storyboard

Is your previous ViewController embedded into a Navigation controller? because "pushing" the view controller automatically adds the back button.

self.navigationController?.pushViewController(viewController: UIViewController, animated: Bool)

How to add a back button and navigation bar to already existing storyboard?

Since you said that you don't want to embed it in navigation controller, you can drag&drop UINavigation Bar from the sidebar:

Sample Image

How to change UINavigationBar Title and Back Button Colour from Storyboard?

What is the best way of changing colors for Title and Back button of UINavigationBar from Storyboard?

The color of the title is an attribute of the navigation bar, so select the navigation controller's navigation bar:

navigation bar selection

and then look at the Attributes Inspector, where you can set the title color:

title color

The color of the Back button is controlled by the tint color. You can set the global tint color in the File Inspector for the storyboard:

global tint color

These settings should work fine if you want to set the title and tint color once for the whole app, but if you want different colors for different view controllers, then one way or another you're going to have to write some code. If that's something you need to do often, and you want to be able to set the colors in IB, you could consider writing your own UIViewController subclass from which all your view controllers are derived. Give that common controller class inspectable attributes for the colors you want to set, and of course add code that sets them appropriately. You'll probably want to use UIAppearance for that.

Bear in mind, though, that the reason that these colors aren't already attributes of UIViewController is that the colors are supposed to help give your app a consistent appearance; changing your app's color scheme from one scene won't be a favor to your users.

Xcode / Swift: How I implement a back button?

When using storyboards the back button is usually implemented with unwind segue.

I usually like to follow raywenderlich toturials on UI related topics, like this - http://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2

It include a detailed example of how to implement back button in storyboards. Quoting from it -

Storyboards provide the ability to ‘go back’ with something called an unwind segue, which you’ll implement next.
There are three main steps:
1. Create an object for the user to select, usually a button.
2. Create an unwind method in the controller that you want to return to.
3. Hook up the method and the object in the storyboard.

Change back button label in navigation bar using Storyboard?

Drag a navigation item to your VC3

Storyboard

How do I add a Back Button to the Navigation Bar through Storyboard?

The default back button is... default :). I'll give you a quick example. Create a new project with a single view. Go into story board, click on the single view controller, then go to Editor in the menu -> Embed in -> Navigation Controller. You'll now see 2 view controllers - one being the navigation controller, the second being the original view controller. You'll notice that the Navigation Controller is now the initial view of the app, and there will be a line connecting the segues, indicating that the view controller is the root view controller of the navigation controller. Then add another UIViewController (by dragging and dropping) next to the original View Controller. Now you'll have 3 View Controller, the Navigation Controller, the original View Controller, and the new View Controller. Add a UIButton on the upper left of the original view controller. Then Control drag from the button to the new view controller and select the show segue. If you now try running the app in simulator, you'll see the original view controller first. Tap the button and you'll see the new view controller with a Back button! You'll get the back button without even trying -- because it is default.

Edit

Based on the comments, the goal is to go straight to the MainViewController if the user is logged in but go to the LogInViewController if the user is not logged in.

Give the push segue from the LogInViewController to the MainViewController an identifier by going to the outline, selecting the segue, go to the identity inspector, and give it an identifier. In ViewDidAppear of the LogInViewController check if the user is logged in. If so, then perform the segue. This will give a back button on the MainViewController to the LogInView Controller.

In my app, I do things slightly differently.

Alternative: I make the LogInViewController the initial View Controller of the app, but do not embed it in a UINavigationController. Then I add the MainViewController to the storyboard and embed it in the Navigation Controller. I am suggesting to embed it in the Navigation Controller in case you have other views you want to show and have a back button to the MainViewController. Add a UIButton to the LogInViewController and control drag now from it to the Navigation Controller. Select the present modally segue. Select the segue in the outline of the storyboard, and in the attributes inspector, give it a name like "present MainViewController". In the ViewDidAppear method of the LogInViewController, check if the user is logged in, and if so call [self performSegueWithIdentifier:@"present MainViewController" sender:nil]; This will present the MainViewController if the user is logged in but not otherwise. There will not be a back button in the MainViewController because it is the root view controller of the Navigation Controller that was presented modally. If you want the upper left button to be a button that logs the user out, you can add a UIBarButton to the left side of the MainViewController. Now go to LogInViewController code and add - (IBAction) nameOfUnwindSegue : (UIStoryboardSegue *) segue{} Go back to the Storyboard and control drag from this bar button to the exit icon on the top of the View Controller in storyboard. Select nameOfUnwindSegue (you can name this whatever you want). This creates an unwind segue which will dismiss the view controller. You can given an identifier by clicking on it in storyboard, going to the attributes inspector, and give it the identifier " In the prepareForSegue method in the MainViewController, you can check if segue.identifier is equal to the identifier and if so, call a log out method.

Swift: Back Button shows in Storyboard but not Simulator

Your initial view controller in the storyboard is actually the TabelViewController (you can see there is an arrow to it).

Thats' why when you start the scene and the TableViewController shows but it is not embedded in the navigation because the navigation controller has never been created.

Just change which is the initial view controller to be the navigation controller or any other before the navigation controller which holds the TableViewController.

You can just drag the arrow to change the initial controller in the storyboard



Related Topics



Leave a reply



Submit