Adding a toolbar to a view

In this recipe, we will learn how to add and use toolbars within our application.

Getting ready

In this recipe, we will discuss the usage of the Toolbar object and how we can use the associated properties to display an animated progress bar.

How to do it...

In order to see how this can be achieved, we need to modify our ViewObjectsExample application that we created in the previous recipe. Perform the following steps to do so:

  1. Open the ViewObjectsExample.xcodeproj project file.
  2. Select the ViewController.xib file from the project navigator window.
  3. From Object Library, drag a Toolbar object into the main view controller, and place it at the top of the main view.
  4. Next, select the button it contains by default and set its Title property to Fade In.
  5. Next, add a Flexible Space Bar Button Item object to the toolbar to the right of the Fade In button.
  6. Then, from Object Library, drag a UIBarButtonItem object to the toolbar and place this after the Flexible Space Bar Button Item object.
  7. Next, select the button and set its Title property to Fade Out.
  8. Next, create the outlets and properties for the Fade In and Fade Out buttons, and name them btnFadeIn and btnFadeOut.
  9. Next, create the action methods for the Fade In and Fade Out buttons, and name them viewFadeIn and viewFadeOut.
  10. After creating the button outlets and properties, save the document by selecting File | Save from the menu bar, or alternatively by pressing command + S. The layout should look like the following screenshot:
    How to do it...

Our next step is to create the code functionality that will be responsible for updating the label to display what button was pressed within the toolbar:

  1. Open the ViewController.m implementation file from the project navigator.
  2. Modify the viewFadeIn and viewFadeOut methods, as shown in the following code snippet:
    - (IBAction)viewFadeIn:(UIBarButtonItem *)sender {
    lblInfo.text = @"Fade In button clicked.";
    }
    
    - (IBAction)viewFadeOut:(UIBarButtonItem *)sender {
    lblInfo.text = @"Fade Out button clicked.";
    }
  3. Then, build and run the application by selecting Product | Run from the Product menu, or alternatively by pressing command + R.

When the compilation completes, the iOS Simulator will appear. Tap on both of the toolbar buttons to see the label update based on which button has been clicked.

How it works…

The UIToolbar object is used to hold items relating to the UIBarButtonItem object type. These types of objects are special kinds of buttons and spacers. A UIBarButtonItem object can be system defined or custom type, and can use any of the predefined types as listed in the Identifier attribute within Interface Builder and provides a specific icon to the button.

The UIBarButtonItem objects can also be customized to contain an image to make your application more professional and visually intuitive. This can be achieved by using the Image attribute within Interface Builder.

There's more…

The UIBarButtonItem class has a style property that determines the button's style. It can be used only when the button item's identifier is set to Custom. You can set the button's style property to any of the UIBarButtonItemStyle types.

See also

  • The Adding and customizing views recipe
  • The Using labels to display text recipe
..................Content has been hidden....................

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