IOS 11 Development Tutorials (14) IOS11 App code add view

Source: Internet
Author: User

IOS 11 Development Tutorials (14) IOS11 App code add view

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 object

Each view is a specific class. In Swift, it is 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:

Let/var Object name = View Class ()

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

Let Newview=uiview ()

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

(2) Setting 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:

Object name. frame=cgrect (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 Newview set location and size:

Newview.frame=cgrect (x:67, y:264, width:240, height:128)

where 67 and 264 represent the position in the main view of the views, 240 and 128 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:

Let Newview=uiview (Frame:cgrect (x:67, y:264, width:240, height:128))

(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 Newview to the current main view, the code is as follows:

Self.view.addSubview (Newview)

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

Import UIKit

Class Viewcontroller:uiviewcontroller {

Override Func Viewdidload () {

Super.viewdidload ()

Additional setup after loading the view, typically from a nib.

Let Newview=uiview (Frame:cgrect (x:67, y:264, width:240, height:128))

Self.view.addSubview (Newview)

}

......

}

When you run the program, you see the effect shown in 1.50. 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 gray:

Newview.backgroundcolor=uicolor.gray

When you run the program, you see the effect shown in 1.51.

Figure 1.50 Run effect figure 1.51 run effect

IOS 11 Development Tutorials (14) IOS11 App code add view

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.