Several Basic knowledge points about UIView and subview-IOS development (instance) and uiview-ios

Source: Internet
Author: User

Several Basic knowledge points about UIView and subview-IOS development (instance) and uiview-ios

The environment is xcode4.3

 

First, you must understand several basic concepts.

 

1) Three structs: CGPoint, CGSize, and CGRect

 

1. CGPoint

C code
  • /* Points .*/
  • Struct CGPoint {
  • CGFloat x;
  • CGFloat y;
  • };
  • Typedef struct CGPoint;
  • /* Points. */    struct CGPoint {    CGFloat x;    CGFloat y;  };  typedef struct CGPoint CGPoint;

    You must have understood this and will not explain it again.

     

    2. CGSize

    C code
  • /* Sizes .*/
  • Struct CGSize {
  • CGFloat width;
  • CGFloat height;
  • };
  • Typedef struct CGSize;
  • /* Sizes. */    struct CGSize {    CGFloat width;    CGFloat height;  };  typedef struct CGSize CGSize; 

    Not explained.

     

    3. CGRect

    C code
  • /* Rectangles .*/
  • Struct CGRect {
  • CGPoint origin; // The offset is relative to the parent window.
  • CGSize size;
  • };
  • Typedef struct CGRect;
  • /* Rectangles. */struct CGRect {CGPoint origin; // The offset is relative to the CGSize of the parent window;}; typedef struct CGRect;

    It is also not explained.

     

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

     

    (2) Several Methods

     

    1. CGRectMake

    C code
  • CG_INLINE CGRect
  • CGRectMake (CGFloat x, CGFloat y, CGFloat width, CGFloat height)
  • {
  • CGRect rect;
  • Rect. origin. x = x; rect. origin. y = y;
  • Rect. size. width = width; rect. size. height = height;
  • Return rect;
  • }
  • CG_INLINE CGRect  CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)  {    CGRect rect;    rect.origin.x = x; rect.origin.y = y;    rect.size.width = width; rect.size.height = height;    return rect;  }  

     

    Yes, this method is to make a rect, set the origin (starting point, top left corner), width and height, you can draw a rect (rectangle) with the position and size determined) 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.

    C code
  • CGrect screenBounds = [[UIScreen mainScreen] bounds]; // A Rect with the status bar is returned.
  • CGRect viewBounds = [[UIScreen mainScreen] applicationFrame]; // Rect that does not contain the status bar
  • // Both screenBounds and viewBounds are relative to the device screen.
  • // Therefore, screenBounds. origin. x = 0.0; screenBounds. oringin. y = 0.0;
  • ScreenBounds. size. width = 320; screenBounds. size. height = 480 (or other resolutions are different)
  • // 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
  • CGrect screenBounds = [[UIScreen mainScreen] bounds]; // The returned result is the Rect CGRect viewBounds with the status bar = [UIScreen mainScreen] applicationFrame]; // Rect that does not contain the status bar // both screenBounds and viewBounds are relative to the device screen // so screenBounds. origin. x == 0.0; screenBounds. oringin. y = 0.0; screenBounds. size. width = 320; screenBounds. size. height = 480 (or other resolutions are different) // 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. I will paste a lot of simple things in the library. If there are too many things to paste it out, it is not very good. Let's see it in the library file. This class is in UIView. h. Next we will first learn some basic things, and other things will be launched later.

     

    C code
  • 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.
  • 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.

     

    We can add this canvas to other canvases. The specific method will be described later. We can also draw other interesting things on this canvas, which will be explained in detail later.

     

    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.

     

    Take a look at the system initialization process (in application didfinishlauchingwitexceptions ):

    C code
  • Self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease];
  • Self. window. backgroundColor = [UIColor grayColor]; // set a background color for the window.
  • [Self. window makeKeyAndVisible]; // display the window
  • Self. window = [[[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds] autorelease]; self. window. backgroundColor = [UIColor grayColor]; // set a background color for window [self. window makeKeyAndVisible]; // display the window

     

    Practical drills:

     

    I will use a comprehensive example to summarize my learning achievements and thoroughly understand what I have learned. I think that since I chose to write code, I need to understand the principle, otherwise, the programmer cannot be a good programmer.

     

    1) create a project and select Empty Application named LW1. 2) in application didfinishlaunchingwitexceptions, you will find that the system has already created a frame. Now we will use the system to help us build a frame, you can also create a frame by yourself, but it is not necessary. forget to mention that an application can only have one frame.

     

    C code
  • -(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
  • {
  • Self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds];
  • // Override point for customization after application launch.
  • CGRect bound = [[UIScreen mainScreen] bounds];
  • NSLog (@ "boundwith: % f boundheight: % f", bound. size. width, bound. size. height );
  • NSLog (@ "boundx: % f boundy: % f", bound. origin. x, bound. origin. y );
  • CGRect appBound = [[UIScreen mainScreen] applicationFrame];
  • NSLog (@ "appBoundwith: % f boundheight: % f", appBound. size. width, appBound. size. height );
  • NSLog (@ "appBoundx: % f boundy: % f", appBound. origin. x, appBound. origin. y );
  • // Paint the first canvas in blue. The size is 320X100.
  • CGRect CGone = CGRectMake (0.0, 0.0, 320,100); // draw a rectangle and initialize the position and size.
  • UIView * v_one = [[UIView alloc] initWithFrame: CGone]; // initialize the view
  • V_one.backgroundColor = [UIColor blueColor]; // blue
  • [Self. window addSubview: v_one]; // directly add it to the frame.
  • // Pay attention to its location in the second part.
  • CGRect CGtwo = CGRectMake (0.0, 100,160,100); // draw a rectangle, initial position, and size.
  • UIView * v_two = [[UIView alloc] initWithFrame: CGtwo]; // initialize the view
  • V_two.backgroundColor = [UIColor redColor]; // red
  • [Self. window addSubview: v_two]; // overlay the image to the frame.
  • // Pay attention to the third part.
  • CGRect CGthree = CGRectMake (160,100,160,100 );//
  • UIView * v_three = [[UIView alloc] initWithFrame: CGthree]; //
  • V_three.backgroundColor = [UIColor greenColor]; //
  • [Self. window addSubview: v_three]; //
  • // Block 4 pay attention to its location
  • CGRect CGfour = CGRectMake (0.0, 260,320,200 );//
  • UIView * v_four = [[UIView alloc] initWithFrame: CGfour]; //
  • V_four.backgroundColor = [UIColor orangeColor]; //
  • [Self. window addSubview: v_four]; //
  • // The fifth part. Calculate its position and check its effect,
  • // You can try to move this code to the First FAST initialization, which has unexpected results.
  • CGRect CGfive = CGRectMake (100,150,160,200 );
  • UIView * v_five = [[UIView alloc] initWithFrame: CGfive];
  • V_five.backgroundColor = [UIColor yellowColor];
  • [Self. window addSubview: v_five];
  • Self. window. backgroundColor = [UIColor grayColor]; //
  • [Self. window makeKeyAndVisible]; //
  • // Remember release
  • V_one = nil;
  • V_two = nil;
  • V_three = nil;
  • V_four = nil;
  • V_five = nil;
  • Return YES;
  • // Self. window. backgroundColor = [UIColor whiteColor];
  • // [Self. window makeKeyAndVisible];
  • // Return YES;
  • }
  • Obtain the size of the image's Work Area

     

    IOS can be executed on many Apple devices. However, the size of the Application Frame provided by each device is not the same. The following provides a simple method, this helps you quickly find out the size of the current work area. The Code is as follows.

     

    The first part is the Status Bar of the Status column.

     

    C code
  • // Obtain the location and size of StatusBar
  • [Self. view addSubview: th1_lbar];
  • CGRect statusBarRect = [[UIApplication sharedApplication] statusBarFrame];
  • NSLog (@ \ "% @ \", NSStringFromCGRect (statusBarRect ));
  • // Obtain the position and size of StatusBar [self. view addSubview: theToolbar]; CGRect statusBarRect = [[UIApplication sharedApplication] statusBarFrame]; NSLog (@ \ "% @ \", NSStringFromCGRect (statusBarRect ));

     

    The size of the work area. If your application contains a Status column, the size of the work area will be the area left by the entire screen minus the Status column.

     

    C code
  • // Obtain the location and size of the work area
  • CGRect workSpaceRect = [[UIScreen mainScreen] applicationFrame];
  • NSLog (@ \ "% @ \", NSStringFromCGRect (workSpaceRect ));
  • // Obtain the location and size of the work area CGRect workSpaceRect = [[UIScreen mainScreen] applicationFrame]; NSLog (@ \ "% @ \", NSStringFromCGRect (workSpaceRect ));

     

    The last step is the size of the entire screen.

     

    C code
  • // Obtain the position and size of the entire Screen
  • CGRect windowRect = [[UIScreen mainScreen] bounds];
  • NSLog (@ \ "% @ \", NSStringFromCGRect (windowRect ));
  • // Obtain the position and size of the entire screen. CGRect windowRect = [[UIScreen mainScreen] bounds]; NSLog (@ \ "% @ \", NSStringFromCGRect (windowRect ));

     

    The above code stores the obtained size range information in the CGRect type variable, and then displays the variable as a string.

    Common methods for managing Subview in UIView

     

    A UIView can contain many subviews (Other uiviews). These subviews have so-called class relationships with each other, which is similar to the concept of layers in the drawing software, the following code demonstrates several common methods on the management layer (Subview). The Code is as follows.

    The first step is to add and remove subviews that are most commonly used.

    C code
  • // Remove the Subview from the current UIView
  • [Subview removeFromSuperview];
  • // Add a Subview for UIView
  • [UIView addSubview: Subview];
  • // Remove the Subview from the current UIView [Subview removeFromSuperview]; // Add a Subview [UIView addSubview: Subview] For the UIView;

     

    In UIView, move the Subview forward or backward to a layer, and move it forward to overwrite the Subview of the lower layer, while move it backward to be overwritten by the Subview of the upper layer.

    C code
  • // Move the Subview forward to a layer (opposite to the previous layer)
  • [UIView bringSubviewToFront: Subview];
  • // Move the Subview back to a layer (opposite to the next layer)
  • [UIView sendSubviewToBack: Subview];
  • // Move the Subview forward to a layer (opposite to the previous layer) [UIView bringSubviewToFront: Subview]; // move the Subview back to a layer (opposite to the next layer) [UIView sendSubviewToBack: Subview];

     

    Use Index indexes in UIView to exchange the layer levels of the two subviews.

    C code
  • // Exchange two layers
  • [UIView exchangeSubviewAtIndex: indexA withSubviewAtIndex: indexB];
  • // Exchange two layers [UIView exchangeSubviewAtIndex: indexA withSubviewAtIndex: indexB];

     

    Use the variable name of the Subview to obtain its Index value (Index) in the UIView ).

    C code
  • // Obtain the Index
  • NSInteger index = [[UIView subviews] indexOfObject: Subview name];
  • // Obtain Index NSInteger index = [[UIView subviews] indexOfObject: Subview name];

     

    Add an NSInteger Tag to the Subview so that they can distinguish each other.

    C code
  • // Add Logging
  • [Subview setTag: NSInteger];
  • // Add the marker [Subview setTag: NSInteger];

     

    The last step is to obtain all the subviews in the UIView. Calling this method will return an NSArray and list the subviews in the forward order, listing all the subviews in the Root of the sample image.

    C code
  • // Obtain all subviews under the UIView
  • [UIView subviews]
  • // Obtain all subviews under the UIView [UIView subviews]

     

     

    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.