iOS Programming (bilingual edition)-View-frame/bounds/center

Source: Internet
Author: User

1. Frame

Each view has a frame property, which is a cgrect structure that describes the position of the rectangle where the view resides in its parent view .

(The default origin of the screen coordinate system is in the upper-left corner, the x-axis stretches to the right, and the y-axis stretches down).

Sets the frame normally through the specified initializer of the view initWithFrame

Let's take a look at an example that initializes 3 rectangular regions that overlap each other.

(OBJECTIVE-C code)

uiview* V1 = [[UIView alloc] Initwithframe:cgrectmake (113,111, the,194)];v1.backgroundcolor= [Uicolor colorwithred:1Green:.4Blue1Alpha1]; UIView* v2 = [[UIView alloc] Initwithframe:cgrectmake ( A, About, the,194)];v2.backgroundcolor= [Uicolor colorwithred:.5Green1Blue0Alpha1]; UIView* V3 = [[UIView alloc] Initwithframe:cgrectmake ( +,197, the, the)];v3.backgroundcolor= [Uicolor colorwithred:1Green0Blue0Alpha1]; [Mainview ADDSUBVIEW:V1]; [V1 addsubview:v2]; [Mainview Addsubview:v3];

(Swift code IOS9)

Let V1 = UIView (Frame:cgrectmake (113,111, the,194)) V1.backgroundcolor= Uicolor (red:1, Green:0.4, Blue:1, Alpha:1) Let v2= UIView (Frame:cgrectmake ( A, About, the,194)) V2.backgroundcolor= Uicolor (red:0.5, Green:1, Blue:0, Alpha:1) Let V3= UIView (Frame:cgrectmake ( +,197, the, the)) V3.backgroundcolor= Uicolor (red:1, Green:0, Blue:0, Alpha:1) Mainview.addsubview (v1) V1.addsubview (v2) Mainview.addsubview (v3)

Operation Result:

2. Bounds

Bounds is also a cgrect structure, unlike frame, which describes the rectangular area of the view itself, relative to its own coordinate system .

The following example creates a rectangular view of 2 overlays with a sub-view of the smaller green

(OBJECTIVE-C code)

uiview* V1 = [[UIView alloc] Initwithframe:cgrectmake (113,111, the,194)];v1.backgroundcolor= [Uicolor colorwithred:1Green:.4Blue1Alpha1];//when drawing inside a view, it is often necessary to use the view's boundsuiview* v2 = [[UIView alloc] Initwithframe:cgrectinset (V1.bounds,Ten,Ten)];v2.backgroundcolor= [Uicolor colorwithred:.5Green1Blue0Alpha1]; [Mainview ADDSUBVIEW:V1]; [V1 addsubview:v2];

(Swift code IOS9)

Let V1 = UIView (Frame:cgrectmake (113,111, the,194)) V1.backgroundcolor= Uicolor (red:1, Green:0.4, Blue:1, Alpha:1)//when drawing inside a view, it is often necessary to use the view's boundsLet V2 = UIView (frame:v1.bounds.insetBy (DX:TenDy:Ten)) V2.backgroundcolor= Uicolor (red:0.5, Green:1, Blue:0, Alpha:1) Mainview.addsubview (v1) V1.addsubview (v2)

Operation Result:

The following example completely overrides the parent view by changing the bounds of the green sub-view

(OBJECTIVE-C code)

uiview* V1 = [[UIView alloc] Initwithframe:cgrectmake (113,111, the,194)];v1.backgroundcolor= [Uicolor colorwithred:1Green:.4Blue1Alpha1]; UIView* v2 = [[UIView alloc] Initwithframe:cgrectinset (V1.bounds,Ten,Ten)];v2.backgroundcolor= [Uicolor colorwithred:.5Green1Blue0Alpha1]; [Mainview ADDSUBVIEW:V1]; [V1 addsubview:v2];//Redefine the bounds of a child viewCGRect r =V2.bounds;r.size.height+= -; R.size.width+= -; V2.bounds= R;

(Swift code IOS9)

Let V1 = UIView (Frame:cgrectmake (113,111, the,194)) V1.backgroundcolor= Uicolor (red:1, Green:0.4, Blue:1, Alpha:1) Let v2= UIView (frame:v1.bounds.insetBy (DX:TenDy:Ten)) V2.backgroundcolor= Uicolor (red:0.5, Green:1, Blue:0, Alpha:1) Mainview.addsubview (v1) V1.addsubview (v2) v2.bounds.size.height+= -V2.bounds.size.width+= -

Operation Result:

In the following example, the origin of the purple parent view is slightly offset.

(OBJECTIVE-C code)

uiview* V1 = [[UIView alloc] Initwithframe:cgrectmake (113,111, the,194)];v1.backgroundcolor= [Uicolor colorwithred:1Green:.4Blue1Alpha1]; UIView* v2 = [[UIView alloc] Initwithframe:cgrectinset (V1.bounds,Ten,Ten)];v2.backgroundcolor= [Uicolor colorwithred:.5Green1Blue0Alpha1]; [Mainview ADDSUBVIEW:V1]; [V1 addsubview:v2];//change the parent view's origin coordinatesCGRect r =v1.bounds;r.origin.x+=Ten; R.ORIGIN.Y+=Ten; V1.bounds= R;

(Swift code IOS9)

Let V1 = UIView (Frame:cgrectmake (113,111, the,194)) V1.backgroundcolor= Uicolor (red:1, Green:0.4, Blue:1, Alpha:1) Let v2= UIView (frame:v1.bounds.insetBy (DX:TenDy:Ten)) V2.backgroundcolor= Uicolor (red:0.5, Green:1, Blue:0, Alpha:1) Mainview.addsubview (v1) V1.addsubview (v2)//change the parent view's origin coordinatesV1.bounds.origin.x + =TenV1.BOUNDS.ORIGIN.Y+=Ten

Operation Result:

3. Center

Center point Location coordinates of the view

4. About the main window and the device screen

The device screen (Uiscreen.mainscreen ()) does not have a frame, but it has bounds.

The main window does not have a parent view, but its frame can be set to the bounds of the screen.

Let w = UIWindow (Frame:UIScreen.mainScreen (). Bounds)

5. About the difference between frame and bounds

Word is the former relative to the parent view, whereas the latter is relative to itself.

Let's take a look at the picture, it's more intuitive.

In the case where the view is not rotated, they are similar, with slightly different coordinates, such as:

The coordinates of the transform are very different after the views have been rotated like this, see:

iOS Programming (bilingual edition)-View-frame/bounds/center

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.