IOS Chapter 4: Basic knowledge about View

Source: Internet
Author: User

Basic knowledge about UIView

 

First, you must understand several basic concepts.

1) Three structs: CGPoint, CGSize, and CGRect

1. CGPoint

 

1./* Points .*/

2.

3. struct CGPoint {

4. CGFloat x;

5. CGFloat y;

6 .};

7. typedef struct CGPoint;

8.

 

2. CGSize

 

1./* Sizes .*/

2.

3. struct CGSize {

4. CGFloat width;

5. CGFloat height;

6 .};

7. typedef struct CGSize;

 

3. CGRect

 

1./* Rectangles .*/

2.

3. struct CGRect {

4. CGPoint origin; // The offset is relative to the parent window.

5. CGSize size;

6 .};

7. typedef struct CGRect;

The three struct types are in the same header file: CGGeometry. h

 

(2) Several Methods

1. CGRectMake

 

1. CG_INLINE CGRect

2. CGRectMake (CGFloat x, CGFloat y, CGFloat width, CGFloat height)

3 .{

4. CGRect rect;

5. rect. origin. x = x; rect. origin. y = y;

6. rect. size. width = width; rect. size. height = height;

7. return rect;

8 .}

 

This method is to make a Rect, determine the origin (starting point, top left corner), width and height, you can draw a rect (rectangle) with the position and size ).

This function is declared as an inline function. The first is because it is relatively small, and the second is because we require certain efficiency when drawing the interface. This function is still hidden in the header file: CGGeometry. h

 

3) Several Basic interface elements: window and view)

To display the content on the screen, you must first create a window bearing content. To create a window, you need a border (frame). The underlying structure containing the border information is CGRect.

Each object that can display itself on the screen has a border that defines its display area. However, many high-level view classes automatically calculate this information. Other classes are set through an initWithFrame initialization method during view initialization.

Let's get to know another class: UIScreen. The UIScreen class represents the screen. Through this class, we can obtain the desired stuff.

 

 

1. CGrect screenBounds = [[UIScreen mainScreen] bounds]; // A Rect with the status bar is returned.

2. CGRect viewBounds = [[UIScreen mainScreen] applicationFrame]; // Rect that does not contain the status bar

3. // both screenBounds and viewBounds are relative to the device screen.

4. // so screenBounds. origin. x = 0.0; screenBounds. oringin. y = 0.0;

5. screenBounds. size. width = 320; screenBounds. size. height = 480 (or other resolutions are different)

6. // so screenBounds. origin. x == 0.0; screenBounds. oringin. y = 20.0; (because the height of the status bar is 20 pixels) screenBounds. size. width = 320; screenBounds. size. height = 480

 

UIView

Next, let's take a look at the UIView class. This class inherits from UIResponder. When we look at this name, we know that it is the canvas for display. If we say that we compare window to a frame. The size of the canvas is determined by the painter. With the canvas, we can apply it as needed.

 

 

1. UIView * myView = [[UIView alloc] initWithFrame: CGRectMake (0.0, 0.0, 200.0, 400.0)]; // a canvas is created to define the position relative to the parent window, and the size.

 

 

UIWindow

UIWindow inherits from UIView. This may be a logical obstacle. How can I inherit a picture frame from the canvas? Do not go too far to the tip of the horn. Isn't the frame in the same shape as the canvas? Take a canvas and use some methods to strengthen it. Can it be used as a frame? This is why a view can be directly added to another view.

 

System initialization process (applicationdidfinishlauchingwitexceptions ):

 

1. self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds] autorelease];

2. self. window. backgroundColor = [UIColor grayColor]; // set a background color for the window.

3. [self. window makeKeyAndVisible]; // display the window

 

-------------------------------------------------------------------------------

Code exercise:

1) create a new project and select "EmptyApplication" named "LLL". In applicationdidfinishlaunchingwitexceptions, you will find that the system has already created a frame. Now we will use the system to create a frame for us, you can also create a frame by yourself, but this is not necessary. An application can only have one frame (UIWindow ).

 

 

1.-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions

2 .{

3. self. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease]; // The system helps you create a frame.

4.

5. // test the difference between bounds and aplicationFrame.

6. CGRect bound = [[UIScreen mainScreen] bounds];

7. NSLog (@ "boundwidth: % f boundheight: % f", bound. size. width, bound. size. height );

8. NSLog (@ "boundx: % f boundy: % f", bound. origin. x, bound. origin. y );

9.

10. CGRect appBound = [[UIScreen mainScreen] applicationFrame];

11. NSLog (@ "appBoundwidth: % f appBoundheight: % f"

12., appBound. size. width, appBound. size. height );

13. NSLog (@ "appBoundx: % f appBoundy: % f", appBound. origin. x, appBound. origin. y );

14.

15. // paint the first canvas in blue. The size is 320X100.

16. CGRect CGone = CGRectMake (0.0, 0.0, 320,100); // draw a rectangle and initialize the position and size.

17. UIView * v_one = [[UIView alloc] initWithFrame: CGone]; // initialize the view

18. v_one.backgroundColor = [UIColor blueColor]; // blue

19. [self. window addSubview: v_one]; // directly add it to the frame.

20.

21. // pay attention to the location of the second part.

22. CGRect CGtwo = CGRectMake (0.0, 100,160,100); // draw a rectangle, initial position, and size.

23. UIView * v_two = [[UIView alloc] initWithFrame: CGtwo]; // initialize the view

24. v_two.backgroundColor = [UIColor redColor]; // red

25. [self. window addSubview: v_two]; // overlay the image to the frame.

26. // pay attention to the third part.

27. CGRect CGthree = CGRectMake (160,100,160,100 );//

28. UIView * v_three = [[UIView alloc] initWithFrame: CGthree]; //

29. v_three.backgroundColor = [UIColor greenColor]; //

30. [self. window addSubview: v_three]; //

31. // pay attention to the position of the fourth part.

32. CGRect CGfour = CGRectMake (0.0, 260,320,200 );//

33. UIView * v_four = [[UIView alloc] initWithFrame: CGfour]; //

34. v_four.backgroundColor = [UIColor orangeColor]; //

35. [self. window addSubview: v_four]; //

36. // The fifth part. Calculate its position and check its effect,

37. You can try to move this code to the First FAST initialization, with unexpected results.

38. CGRect CGfive = CGRectMake (100,150,160,200 );

39. UIView * v_five = [[UIView alloc] initWithFrame: CGfive];

40. v_five.backgroundColor = [UIColor yellowColor];

41. [self. window addSubview: v_five];

42. self. window. backgroundColor = [UIColor grayColor]; //

43. [self. window makeKeyAndVisible]; //

44. // remember release

45. [v_one release];

46. [v_two release];

47. [v_three release];

48. [v_four release];

49. [v_five release];

50. return YES;

51 .}

 

 


 

Are you seeing the colorful blocks? You can accurately calculate the size and position. There are several changes, similar to block location in the webpage with CSS + DIV.

 

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.