官术网_书友最值得收藏!

Creating the UI

In this recipe, we will learn how to add and manage controls in the user interface.

Getting ready

Let's add a few controls in an interface. Start by creating a new iPhone single view application project in Xamarin Studio. Name the project ButtonInput. When it opens, double-click on ButtonInputViewController.xib in the Solution pad to open it with Interface Builder.

How to do it...

Now that we have a new project, and Interface Builder has opened the ButtonInputViewController.xib file, we'll add some controls to it.

Adding a label

Perform the following steps:

  1. Go to the Library pane and select Objects from the drop-down list, if it is not already selected.
  2. Select the Label object. Drag-and-drop Label onto the gray space of the view in the designer, somewhere in the top half.
  3. Select and resize the Label object from both the left and right sides so that it snaps to the dashed line that will show up when you reach close to the edges of the view.
  4. Again, with the Label object selected, go to the Inspector pane, select the Attributes tab, and in the Layout section, click on the middle alignment button.

Congratulations, you have just added Label in your app's main view!

Adding a button

We will perform similar steps to add a button in our interface, using the following steps:

  1. Again, in the Library pane, in the Objects section, select the Button object. It is next to the Label object. Drag-and-drop it onto the bottom half of the view. Align its center with the center of Label we added earlier.
  2. A dashed line will show up, and the Button object will snap to it when the centers of the two controls are almost aligned.
  3. Resize the Button object to the same width as that of Label. Since Label has a transparent background, and you cannot see how wide it is exactly, you will know when the Button object is of the same width when three dashed lines show up while you are resizing it.
  4. Now, let's add some text to Button. Select it and go to Inspector pane.
  5. In the Attributes tab of the Title field, enter Tap here please!.
  6. After adding the button, save the document by navigating to File | Save in the menu bar. The main view should now look like the following screenshot:
    Adding a button

How it works...

As you can see, although some concepts of Interface Builder seem difficult, it is quite easy to use. It also provides a lot of feedback. When we drag objects, guidelines that basically act as snap points for positioning the control properly, appear. Also, when we resize a control, we see its dimensions next to it.

You can also resize and position the controls by modifying the values in the Size tab of the Inspector pane. Another useful feature in the Size tab is Autosizing. Autosizing provides layout options for the controls, and it can be very useful when we want our app to support different device orientations. You can select a control you want, and then click on the lines that are outside or inside of the square on the left in the Autosizing section. The image next to it animates to give you an impression of how the control will behave when the layout changes.

There's more...

Now, let's try running the app on iOS Simulator. Back in Xamarin Studio, select the project configuration Debug if it is not already selected. Depending on the version of iOS SDK you have installed, the following screenshot shows the available options of debugging targets:

There's more...

Select your preferred target and click on the Run button. When the compilation finishes, iOS Simulator will automatically start and run the app we just created. You can even tap on Button by clicking on it with the mouse, and see it responding. Of course, our app does not have any other functionality right now.

Setting titles on Buttons

Setting the title of a Button or Label can be done by simply double-clicking on it and typing the preferable title. Do it, and watch how Interface Builder behaves to show you what action is to be performed.

See also

  • The Compiling an iOS project and Debugging our application recipes
  • The Receiving user input with buttons recipe in Chapter 2, User Interface – Views
主站蜘蛛池模板: 辽宁省| 安顺市| 盐山县| 砚山县| 泰兴市| 绿春县| 英超| 屏边| 天台县| 潞城市| 通辽市| 温州市| 眉山市| 江川县| 泗阳县| 武功县| 松滋市| 沅江市| 天津市| 寻乌县| 勃利县| 楚雄市| 拜泉县| 沁阳市| 木里| 永嘉县| 九龙坡区| 孙吴县| 焦作市| 宜良县| 莱芜市| 纳雍县| 麦盖提县| 南乐县| 五大连池市| 海南省| 将乐县| 德钦县| 美姑县| 冕宁县| 邛崃市|