Replace word: Ehab Yosry Amer up to date this tutorial for iOS 13, Xcode 11 and Swift 5. Nicholas Sakaimbo wrote the unique.
Storyboards are an thrilling function first launched in iOS 5, which save time constructing consumer interfaces in your apps. Storyboards will let you prototype and design a number of view controller views inside one file, and likewise allow you to create transitions between view controllers.
Earlier than storyboards, you had to make use of XIB information (aka NIB information). You could possibly solely use one XIB file per view (for instance, per UITableViewCell, UITableView or different supported UIView sorts).
The next picture exhibits you what a storyboard seems like. It’s just like the storyboard you’ll construct throughout this tutorial:
You could not know what this app does, however you’ll be able to see its scenes and the way they’re associated.
On this tutorial, you’re going to construct Scores, a prototype app that exhibits a listing of gamers, the video games they play and their ability ranking. You’ll study widespread duties that you could accomplish utilizing storyboards, like creating scenes and wiring up view controllers. And also you’ll do all of this with no single line of code. :]
Obtain every thing you want for this tutorial by utilizing the Obtain Supplies button on the prime or backside of this web page. There’s no starter venture, however there’s a folder named Photographs containing property that you simply’ll want later.
Now, open Xcode and create a brand new iOS app utilizing the Single View Software template.
Fill within the template choices as follows, then click on Subsequent after which Create:
Product Title: Scores.
Group Title: Fill this in nonetheless you want.
Group Identifier: The identifier you utilize in your apps.
Person Interface: Storyboard.
Be sure to’ve unchecked the Use Core Knowledge, Embody Unit Assessments and UI Assessments choices.
After you end, the principle Xcode window ought to seem like this:
The brand new venture consists of six information:
Predominant.storyboard, the star of this tutorial
You don’t want to alter something within the .swift information on this tutorial, so don’t fear about them.
Beneath Deployment Information within the Common venture settings, uncheck iPad. Discover System Orientation and, since this can be a portrait-only app, uncheck the Panorama Left and Panorama Proper choices.
Understanding Storyboard Previews
Open Predominant.storyboard within the Challenge navigator to view it within the Interface Builder editor:
Right here, you see a single view controller containing an empty view. The arrow pointing to the view controller from the left signifies it’s the preliminary view controller for this storyboard. You’ll find out about this in additional element later.
You’ll discover the default scene dimension is for a four.7-inch display screen. Xcode permits Auto Format and Dimension Courses by default for storyboards. They will let you make versatile consumer interfaces that may resize simply, which is beneficial for supporting varied sizes of iPhones and iPads.
To vary the scene dimension to a different system, click on the button on the bottom-left of the storyboard. You’ll then be capable of choose from the total vary of supported system sizes, from the iPad Professional (12.9-inch) to the iPhone 4s (three.5-inch), in each portrait and panorama orientations.
Designing Your Storyboard
To design the structure within the storyboard editor, drag controls from the Object Library and drop them into your scene within the place you need. You may change their place or take away them later.
You’ll rely closely on the Object Library to design your storyboards. Earlier than you begin on the app, there’s yet one more merchandise on the display screen you must learn about: The Doc Define.
The Doc Define exhibits all of the objects you’ve got contained in the open storyboard file, in addition to any view controller and any of the controls it consists of. View controllers are referred to as scenes on the storyboard. On this tutorial, you’ll use the identify “scene” to consult with the view controller inside your storyboard.
The Doc Define might be certainly one of your most important navigation factors to pick out the weather of the storyboard.
Now, it’s time to start out constructing the Scores app.
Simply Add It to My Tab
The app you’re about to construct has a tabbed interface with two scenes. So as to add the tabbed interface, begin by opening Predominant.storyboard and deleting the one scene there. Merely click on on View Controller within the Doc Define and press the delete key in your keyboard.
Drag a tab bar controller from the Object Library into the canvas. You may filter the record by typing a part of the identify of the merchandise you’re on the lookout for.
Tab bar controller comes pre-configured with two further view controllers, one for every tab. It’s a so-called container view controller as a result of it incorporates a number of different view controllers. Different widespread containers are the Navigation controller and the Break up View controller.
The arrows between the tab bar controller and the view controllers it incorporates signify the container relationship. The icon proven under, in the midst of the arrow physique, signifies that they’ve an embed relationship.
Notice: If you wish to transfer the tab bar controller and its hooked up view controllers as a gaggle, zoom out, then Command-click or click on and drag to pick out a number of scenes. This allows you to transfer them round collectively. A skinny blue define signifies the scenes you’ve chosen.
Construct and run and also you’ll see one thing like this within the console:
Scores[9912:704408] [WindowScene] Did not instantiate the default view controller for UIMainStoryboardFile ‘Predominant’ – maybe the designated entry level shouldn’t be set?
Don’t panic! :] That error merely signifies that the app didn’t discover the preliminary view controller to indicate. Earlier than you repair this, take a second to grasp what’s happening.
Understanding the Error
First, the error message talked about the identify of your storyboard, Predominant, explicitly. It’s not a key phrase, so how did Xcode know that that is the storyboard file it ought to be loading in the beginning of your app?
To know that, open the appliance settings by deciding on the venture file from the Challenge navigator, deciding on Scores from the Targets record and ensuring that you simply’ve chosen the Common tab on the prime.
On this settings display screen, you’ll see that the worth within the drop-down for Predominant Interface is Predominant. This drop-down will solely present the storyboard information in your venture.
Specifying the Preliminary View Controller
Preserve Predominant as the chosen worth and transfer to the subsequent step: Figuring out the particular view controller you wish to begin with inside that storyboard.
Open Predominant.storyboard and choose the Tab Bar Controller Scene. On the best, choose the Attribute inspector.
You’ll discover a checkbox named Is Preliminary View Controller.
Checking this field will determine the chosen view controller because the preliminary entry level for the storyboard you’re on. Additionally, an arrow will seem on the left of the view controller.
Now, construct and run and also you’ll see an empty view controller with a tab bar that has two objects on the backside.
Notice: One other option to change the preliminary view controller is to pull the arrow between the view controllers.
Xcode comes with a template for constructing a tabbed app: The Tabbed Software template. You could possibly have used it right here, but it surely’s good to know easy methods to construct one your self so you’ll be able to create a tab bar controller by hand, if it’s important to.
Notice: When you join greater than 5 scenes to the tab bar controller, it mechanically provides a Extra tab while you run the app. Fairly neat!
Constructing the Gamers Checklist
It’s time to construct the primary display screen in your app. At present, the 2 screens hooked up to the tab bar controller are each UIViewController cases. You’re going to interchange the primary tab scene with a UITableViewController as a substitute.
Click on the primary view controller within the Doc Define to pick out it, then delete it. Drag a brand new desk view controller into the canvas the place the earlier scene was once:
Creating the Navigation Controller
You wish to place the desk view controller inside a navigation controller. Choose the desk view controller and select Editor ▸ Embed In ▸ Navigation Controller from Xcode’s menu bar. This provides one other controller to the canvas:
You could possibly have dragged in a navigation controller from the library and embedded the desk view, however this Embed In command is a pleasant time-saver for a standard motion.
Navigation controller can be a container view controller, and it has a relationship arrow pointing to the desk view controller. You may see it within the Doc Define:
Discover that embedding the desk view controller gave it a navigation bar. Interface Builder mechanically put it there as a result of this scene will now seem contained in the navigation controller’s context.
Reconnecting the Tab Controller
Deleting the primary scene additionally deleted its relationship with the tab bar controller. However now, you wish to recreate it. As a substitute of connecting it to the Desk View Controller Scene, you’ll join it to the Navigation Controller Scene.
To do that, Management-drag from the tab bar controller to the navigation controller. Once you let go, a small pop-up menu will seem. Select the Relationship Segue – view controllers possibility:
This creates a brand new relationship arrow between the 2 scenes. That is additionally an embed relationship, just like the one you noticed earlier. The tab bar controller has two embed relationships, one for every tab. The navigation controller itself has an embed relationship with the desk view controller.
Once you made this new connection, a brand new tab was added to the tab bar controller named Merchandise. For this app, you need this new scene to be the primary tab, so drag the tabs round to alter their order:
Construct and run to attempt it out. The primary tab now incorporates a desk view inside a navigation controller.
Giving Tabs an Identification
Of their present state, the tabs aren’t expressive in any respect. Every ought to have its personal icon and identify to signify its views. The primary tab’s identify ought to be “Gamers” and the second ought to be “Gestures”.
When any a controller is related to a tab, it mechanically has an occasion of UITabBarItem. This occasion defines the identify and the picture that ought to seem on the tab bar.
Within the Doc Define, underneath the Merchandise 2 Scene, you’ll discover an merchandise named Merchandise 2 that has a star icon beside it. Choose it and, within the Attributes inspector, change its Title to Gestures.
Now, attempt one other option to set the title. Within the navigation controller, double-click the phrase Merchandise on the backside of the tab bar and sort the brand new identify Gamers.
You may add the photographs now. Open Property.xcassets from the Challenge navigator then drag the Photographs folder from the obtain supplies into it.
Again in Predominant.storyboard, use the Attributes inspector to alter the photographs of the 2 tab objects you simply renamed utilizing their matching pictures.
Construct and run and marvel at your fairly tab bar. As promised, you haven’t wanted a single line of code to come back this far… and also you’re simply getting began. :]
Utilizing Desk Cells
To this point, the Gamers tab exhibits an empty record as a result of the desk view on this display screen has no cells. There are two methods a desk view can function:
Dynamic prototypes will let you construct cells on the storyboard to create a number of copies of them. You may solely instantiate them by means of code.
Static cells will seem precisely the way in which you designed them within the storyboard. They don’t want any code to instantiate.
Normally, you’ll use dynamic prototypes in your apps, however because the goal of this tutorial is to construct a prototype with zero code, you’ll use static cells as a substitute. Don’t fear, it’s simple to change between them while you begin constructing the code.
Choose the desk view and, from the Attributes inspector, change the worth of the Content material drop-down to Static Cells. Now, you’ll be able to customise the cells.
Customizing Gamers’ Cells
The storyboard now exhibits three empty cells within the desk view. Go forward and delete two of them.
Choose the remaining cell and alter its Accent to Disclosure Indicator.
Drag a Horizontal Stack View onto your cell. Then, throughout the horizontal stack, add a Vertical Stack View and an Picture View. Lastly, add two labels throughout the Vertical Stack View.
Your Doc Define ought to seem like this now.
Wait, what’s that pink circle doing there? That’s a warning that the views you added don’t know easy methods to place themselves within the cell. As your app runs on totally different gadgets with totally different display screen sizes, the cell’s dimension will change. That is the place Auto Format is available in.
Choose the Horizontal Stack view you added within the cell, then click on on the Add New Constraints button that appears like a tie fighter.
That button will open a dialog that allows you to specify constraint values. Specify the constraints so as to add for the Stack view as follows:
Once you’re accomplished, choose the picture and provides it a Width constraint of 81.
Fixing the Format Points
With every set of constraints you add, the controls on the storyboard alter their positions to match.
It’s vital to keep away from any structure errors, and proper now, there’s nonetheless an error. Typically, Xcode is ready to counsel the constraints you should add to repair it. On this case, these ideas will work, however keep in mind that Xcode gained’t at all times be proper in future instances. :]
Click on on the small pink circle and it’ll present you the structure errors. Click on the brand new pink circle and faucet Change Precedence.
Change the font dimension of the decrease label to 14 and set the picture of the Picture view to 4Stars. Then, use the Dimension inspector to alter the default peak of the cells within the desk view to 60 and uncheck Automated for each the desk view and the cell.
Including Gadgets to Your Checklist
An inventory isn’t actually a listing when it solely has a single merchandise. You may both duplicate the cells you simply created or inform the desk view to indicate extra. On this case, you’ll do the latter.
Choose Desk View Part from the Doc Define and, within the Attributes inspector, set the variety of Rows to three.
Your final step is to present your cells some actual knowledge. Enter some folks’s names and a few video games, then change some pictures to price them. And don’t overlook to present this display screen a title: Simply double-click on the empty house above the desk view and sort Gamers.
Construct and run; you must see the record you simply created.
The next step is to construct a view to let the consumer add a brand new participant.
Constructing the Add Participant Scenes
This display screen won’t be practical with out code to make it work, however nothing is holding you again from creating the interface for these varieties.
To start out, you’ll add a “+” button within the top-right nook of the Gamers Scene.
Add a Bar Button Merchandise to the upper-right nook of the scene. The nook will spotlight mechanically as you drag the button over that space. Then, change its System Sort to Add.
The Add button ought to open a brand new scene with a kind for the consumer to enter the participant’s info; you’ll create that kind subsequent.
Connecting Scenes By way of Segues
Drag a navigation controller onto your scene. It can include a desk view controller. Then, control-drag from the Add button to the newly-added navigation controller. Choose Present from the pop-up menu.
Construct and run, then faucet the Add button.
You will note the brand new view controller seem from the underside of the display screen. Once you drag or swipe that scene down from its header, you dismiss it.
The connection you created from the Add button to the navigation controller is named a Segue. And because it got here from an interactive factor on the display screen, particularly the button, you set off the segue by tapping the button it’s related to.
Apple launched this card-like presentation in iOS 13. Beforehand, any view controller introduced like this might take the total display screen, and also you wanted a few strains of code to dismiss it. So iOS 13 made issues look nicer with no code in any respect … That’s what’s referred to as a double win! :]
Developing the Kind
The shape you wish to create ought to seem like this:
Within the newly-created view controller, double-click on its title from the storyboard and alter it to Add Participant. Then add two Bar Button Merchandise cases, one to the upper-left nook and one to the upper-right nook. Change the System Sort of the left one to Cancel and the one on the best to Executed.
Subsequent, choose the desk view in that scene, change its Content material to Static Cells and alter Sections to 2. Every part has three rows. You solely want one underneath every part, so delete the final two.
Lastly, change the Model of the desk view to Grouped, the header of the primary part to Participant identify and use an empty string for the title of the second.
Construct and run, then faucet the + button to open the Add Participant kind. It ought to seem like this:
Notice: If the background of the desk view was nonetheless white after altering its type to Grouped, then change its Background colour to Default within the View part of the Attributes inspector and it ought to return to mild grey.
Setting Up the Cells
The cell within the first part ought to be a textual content area. So drop a Textual content Discipline on the cell. Then change the Border type of that area to None.
As soon as you modify its type, the management corners of the bounding field of the textual content area will seem. Manually set its dimension to suit the cell – sure with out constraints – it would work. :]
Within the first desk view you created, you added every factor you wished contained in the cell. Xcode additionally offers just a few presets for desk cells that you could select from. They could not at all times suit your wants, however in some easy instances, they match properly.
To attempt them out, choose the cell within the second part then change its Model to Proper Element and the Accent to Disclosure Indicator. Double-click the phrase Title and change it with Recreation.
Construct and run and verify that your kind seems precisely like this:
Now, you’ll be able to populate the record of video games.
Creating the Video games Checklist
Once you faucet the second cell, it ought to open the record of obtainable video games to decide on. This record ought to seem like this:
This wants one other desk view controller, so add one to your storyboard now. Change the Content material of its desk view to Static Cells, and alter the Model of the cells already inside it to Primary. Then choose the Desk View Part and enhance the variety of rows to 6.
Change the titles of the rows to some video games you want, and for less than certainly one of them, change the Accent to Checkmark.
To this point, this scene has no entry factors; you need it to open while you faucet the second cell within the Add Participant display screen. To make that occur, you’ll join the scene the identical approach you related the Add button to the navigation controller.
Management-drag from the second cell to the brand new scene. Choose Present from the pop-up.
Discover that a again button appeared within the second scene after you made the connection. It is because the Present motion right here will push the second scene to the navigation controller that’s already there. Navigation controllers mechanically present a again button to any further view controllers which might be pushed inside them.
Construct and run and have fun the Scores app. As promised, you didn’t want a single line of code to perform a lot. :]
Do you know you may also use gestures in storyboards? Right here’s how.
Making a Gesture
Gestures are one other management you should use immediately on storyboards. On this part, you’ll discover ways to use them… with none code, as ordinary. :]
Add two Swipe Gesture Recognizers to the Gestures Scene.
Notice: Be sure to drop the recognizers on the view of the scene.
Within the Attributes inspector, set the Swipe course of the primary to Left, and the second to Proper.
Discover that their names within the Doc Define are equivalent, though every has a distinct course. This makes it barely more durable to determine your objects afterward. Proper now, it doesn’t really feel arduous since you keep in mind their order, however will you continue to keep in mind it tomorrow?
Labeling Storyboard Components
A greater identify would make it easier to keep in mind what your gestures do. Xcode permits you to change the identify that seems within the Doc Define, which has no impact at runtime.
To vary the identify, choose the primary recognizer then, within the Identification inspector, change the worth of the Label to Swipe Left Gesture. Do the identical factor with the second recognizer to alter the identify to Swipe Proper Gesture.
Their names will now seem like this:
Now that you simply’ve added the swipe gestures they usually’re simply recognizable, you can begin making some use for them. Add two new view controllers and add a label within the middle of every to determine them after they’re open.
Connecting the Gestures
Choose your Swipe Left Gesture and open the Connections inspector. Within the Triggered Segues part, drag from the small circle on the best of motion to the view controller you wish to open from that swipe. Choose Present from the pop-up.
Do the identical for the Swipe Proper Gesture and the opposite view controller.
Construct and run, then choose the Gestures tab and swipe left or proper. The swipe motion will current the view controllers they’re related to. Once you swipe the brand new scene down, it would shut.
Altering the Presentation Animation
UIKit provides totally different presentation kinds and animations, and you may change your type immediately from the storyboard. Choose any of the 2 new view controllers and open the Attributes inspector.
The Transition Model defines how the scene will animate when it presents. And the Presentation units the way in which this scene shows, whatever the animation sort.
Not all Transition Model animations work with all Presentation values. For instance, Partial Curve gained’t work with the brand new presentation type supplied by iOS 13. To attempt it, you have to change Presentation to Full Display screen.
Be at liberty to discover them and see the totally different animations you’ll be able to carry out by merely altering a worth from a drop-down record. All this with none code in any respect. :]
The place to Go From Right here?
On this tutorial, you discovered rather a lot concerning the energy of storyboards, together with:
Constructing your interface utilizing the Object Library and customizing that interface.
Connecting screens by means of segues and making your app interactive with out writing any code.
Customizing names and labels within the Doc Define.
Altering the transition animations between view controllers.
If you wish to construct sooner and extra highly effective interfaces, you’ll wish to study extra about Auto Format. Auto Format is a really highly effective software to outline the structure of your screens, the higher you might be with it, the extra refined your apps might be.
You probably have any questions or feedback, please don’t hesitate to hitch the discussion board dialogue under.