Xamarin iOS Developer manual-----2.2.2 Using code to add views

Source: Internet
Author: User

Xamarin iOS Developer manual-----2.2.2 Using code to add views

What if the developer wants to add views to the main view using code? The following will solve this problem for developers. To add views to the main view using code, you need to implement 3 steps.

1. Materialized View objects

Each view is a specific class. In C #, it's often said that a class is an abstract concept, not a specific thing, so you instantiate the class. The specific syntax for instantiating a View object is as follows:

View class object name =new view class ();

In the first view we contacted, for example, it instantiated the object as follows:

UIView vv=new UIView ();

Where UIView is the class of a blank view, and VV is an object instantiated by the UIView class.

2. Set the location and size of the view

Each view is a region, so you need to set the location and size for this area. Set the position and size of the property as frame, which has the following grammatical form:

The name of the object. Frame=new RectangleF (X, Y, width,height);

where x and y represent the position of the view in the main master, and width and height represent the size of the view. The following is an instantiated object VV set location and size:

vv. Frame = new RectangleF (0, 0, 320, 580);

where 0 and 0 represent the position in the main view of the views, 320 and 580 represent the size of this view.

Note: Step 1 and Step 2 can also be merged. For example, the following code merges an instantiated object and a set location size for the UIView class:

UIView VV = Newuiview (new RectangleF (0, 0, $));

3. Add a view to the current view

Finally, the most critical step is to add the instantiated object to the main view. So that it can be displayed. You need to use the Addsubview () method at this point, which has the following syntax:

This. View.addsubview (View object name);

The following adds the instantiated object vv to the current main view, the code is as follows:

This. View.addsubview (VV);

Example 2-2 The following will use code to add a view blank view to the main page. The code is as follows:

Using system;using system.drawing;using monotouch.foundation;using monotouch.uikit;namespace Application{         Publicpartial class __2viewcontroller:uiviewcontroller         {...         This omits the construction method and the destructor method of the View controller (the basic function of the view controller is to handle the interaction with the view, which we will explain later)                   #regionView lifecycle                   publicoverride void Viewdidload ()                   {                            base. Viewdidload ();                            UIView vv = new UIView ();                                                             Instantiates the object                            vv. Frame= New RectangleF (0, 0, 580);                             Sets the size and position of the View object this                            . View.addsubview (vv);                                                            Add a View object to the current View                   } ...         Some methods before and after view loading and unloading are omitted                   #endregion         }}

Run effect 2.5 as shown.


Figure 2.5 Running effect

The added view is also not visible in this run effect. This is because the added view defaults to a white background, and if you want to see the view, you need to set its background. For example, the following code sets the background color to light gray:

vv.                                                                          BackgroundColor = Uicolor.lightgray; Set the background to light gray

Run effect 2.6 as shown.


Figure 2.6 Running effect

This data is derived from the "Xamarin iOS Development Real-life book" The World's only Chinese books Xamarin learning materials!!! Welcome to Inquire!!

Xamarin iOS Developer manual-----2.2.2 Using code to add a view

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.