Adding a Camera Button

In this section, you will add a way for the user to initiate the photo selection process. You will create an instance of UIToolbar, add a camera button to the toolbar, and place the toolbar at the bottom of DetailViewController’s view.

Open LootLogger.xcodeproj and navigate to Main.storyboard. In the detail view controller, select the bottom constraint for the outer stack view and press Delete to remove it. The stack view will resize itself, which will make some room for the toolbar at the bottom of the screen.

Now, drag a toolbar from the library and place it near the bottom of the view. Make sure it is above the Home indicator (the black bar along the bottom of the screen, shown in Figure 14.4).

Figure 14.4  Adding a toolbar to the detail view controller

Adding a toolbar to the detail view controller

You want the toolbar to extend from the superview’s leading edge to its trailing edge, independent of the safe area. To do this, select both the toolbar and the superview and open the Auto Layout Align menu. Configure the constraints as shown in Figure 14.5 and then click Add 2 Constraints.

Figure 14.5  Toolbar horizontal constraints

Toolbar horizontal constraints

For the vertical constraints, you want the toolbar to be aligned to the bottom safe area and be 8 points away from the stack view. Select only the toolbar this time and open the Auto Layout Add New Constraints menu. Configure the top and bottom constraints as shown in Figure 14.6 and then click Add 2 Constraints.

Figure 14.6  Toolbar vertical constraints

Toolbar vertical constraints

A UIToolbar works a lot like a UINavigationBar – you can add instances of UIBarButtonItem to it. However, where a navigation bar has two slots for bar button items, a toolbar has an array of bar button items. You can place as many bar button items in a toolbar as can fit on the screen.

By default, a new instance of UIToolbar that is created in an interface file comes with one UIBarButtonItem. Select this bar button item and open the attributes inspector. Change the System Item to Camera, and the item will show a camera icon (Figure 14.7).

Figure 14.7  UIToolbar with camera bar button item

UIToolbar with camera bar button item

Build and run the application and navigate to an item’s details to see the toolbar with its camera bar button item.

You have not connected the camera button yet, so tapping it will not do anything. The camera button needs a target and an action. With Main.storyboard still open, Option-click DetailViewController.swift in the project navigator to open it in another editor.

In Main.storyboard, select the camera button in the document outline and Control-drag from the selected button to the DetailViewController.swift editor.

In the panel, select Action as the Connection, name it choosePhotoSource, select UIBarButtonItem as the Type, and click Connect (Figure 14.8).

Figure 14.8  Creating an action

Creating an action

If you made any mistakes while making this connection, you will need to open Main.storyboard and disconnect the bad connection before trying again. (Look for yellow warning signs in the connections inspector.)

With the toolbar and camera button in place, you can now implement the functionality to let the user choose a photo source.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset