UIView Learning Knowledge points

Source: Internet
Author: User

The first week of UI learning

UIView View class: Represents a rectangular area on the screen, and any element you see on the screen is a subclass of UIView or UIView

Several major elements of creating UIView:

1. Size – Width and height

2. Location – The coordinates of the upper-left corner of the view, X (horizontal), Y (ordinate)

3. How to quickly create a variable for a struct

CGRect-Cgrecetmake () contains location and size

Cgpoint-Cgpointmake () contains position x and Y

Cgsize-Cgsizemake () contains size width and height

Instance:

1, creating the View object

UIView *redview = [[UIView alloc] Initwithframe:cgrectmake (0, 0, 100, 100)];

2. Change the background color

Redview.backgroundcolor = [Uicolor Redcolor];

(In this position, the view can be set to uniquely identify the tag

Redview.tag = 101; Can be taken to Redview view via tag value)

3, added to the parent view--on window

[Self.window Addsubview:redview];

4, release ownership--corresponds to alloc operation

[Redview release];

UIView View-related properties

    1. Centerview.hidden = YES; Implicit, yes is shown, no is hidden
    2. Rightupview.alpha = 0.2; Transparency, the range of values between 0–1.0
    3. BackgroundColor: Background color
    4. Superview: Gets the parent view of a view, a view can have only one parent view
    5. Subviews: Gets all the child views of a view. Nsarray. The position of the child view of the array is consistent with the order in which it is added
    6. Tag: The unique identity of the view, is an integer value, generally to more than 100

Get view by Tag value

UIView *tempview = [Self.window viewwithtag:101];

Print view

Print all the child views of the window and center view

NSLog (@ "window:%@", self.window.subviews);

NSLog (@ "window:%@", centerview.subviews);

Print the parent view of the middle view and the upper right corner view

NSLog (@ "center:%@", Centerview.superview);

NSLog (@ "leftup:%@", Leftupview.superview);

Get through Window's subviews

UIView *tempview = [Self.window.subviews objectatindex:1];

API for View operations

Use the Create View method to create a red-yellow-blue-green view, respectively:

Add blue to the middle of the yellow and red views

[Self.window Insertsubview:blueview Belowsubview:yellowview];

[Self.window Insertsubview:blueview Abovesubview:redview];

[Self.window Insertsubview:blueview atindex:1];

This method adds the view to the last position in the subviews array

[Self.window Addsubview:blueview];

[Blueview release];

1. Move the green view to the last face

[Self.window Sendsubviewtoback:blueview];

2. Move the green view to the front

[Self.window Bringsubviewtofront:blueview];

3. Replace the blue and red view positions

[Self.window Exchangesubviewatindex:3 withsubviewatindex:0];

4. Remove the Blue view

[Blueview Removefromsuperview];

Manage hierarchical relationships for views:

    1. A view can have only one parent view, but there may be multiple child views
    2. The position of the elements in the view subviews and the child views are added in the same order, and the later added view is in the last position in the array.
    3. The view that was added late will overwrite the previous view if it overlaps the previous one.
    4. Child views are always displayed in front of the parent view
    5. Adding a child view and adjusting the hierarchical relationship of the view requires that the corresponding method be called by the parent view
    6. If you want to remove a view, call the Remocefromsuperview method directly from the removed view.

View has and position-dependent properties

1.frame: Contains both the size and position, and the position is the coordinate of the point in the upper-left corner of the view area, relative to the parent view's coordinate origin.

2.center: The location of the center point.

3 (difficulty). Bounds: Contains both the size and the position, and the position is the coordinate of the point in the upper-left corner of the view area, relative to the distance from the origin of its own view coordinates.

Change the x, y in bounds. Causes the location of the origin of the view coordinates to change, affecting the child view position.

Instance:

Layout idea-Create a large content view, such as a screen, to add to the window, and all the child views are added to the content view.

UIView *containerview = [[UIView alloc] Initwithframe:[uiscreen mainscreen].bounds];

Containerview.backgroundcolor = [Uicolor Yellowcolor];

[Self.window Addsubview:containerview];

[Containerview release];

Next, add all the child views to the content view Containerview

UIView Learning Knowledge points

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.