"iOS Dev-14" UIView Properties, parent view and child view level actions, child view adaptive mode, plus a timer

Source: Internet
Author: User

(1) UIView view frame settings, four parameters, first 2 determine position, and after 2 determine size.


(2) UIView content mode Contentmode, and in UIImage said is the same, and in the UIImage show easier to understand.


(3) UIView The most important thing is the relationship between the parent and child views, and the parent View Action sub-view (add a sub-view, put a child view at the bottom of the topmost, swap the loading order of two sub-views, etc.)


(4) Another important thing is that if the parent view changes, how the child view automatically adjusts the layout: first let the parent view allow the child view to do this, that is, the parent view's property autoresizessubviews set Yes, Then, the corresponding sub-view to automatically adjust the layout to set an adaptive mode (there are many modes, such as the change itself and high to ensure that the distance up, down, left, right, or to maintain their own aspect unchanged to change the distance between the left and right margin).


(5) A timer is also used here. This is explained in OC, as a review.


#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller{UIView *view6;//in order to change its size,    Set as a global variable so that you can use}-(void) Viewdidload {///Instantiate an object UIView *view1=[[uiview Alloc]init] in other methods;    Set position and size, the position should pay attention to the above status bar occupies 20, so y to greater than 20, or is obscured by the status bar, under the status bar text, visual poor//because it is loaded in the entire view of the Self.view, so the x and Y position is relative to the entire interface    View1.frame=cgrectmake (10, 30, 100, 100); CGRectMake is a cgrect structure that includes the size,cgpoint structure of origin and cgsize structure of the cgpoint structure with x and Y, so origin also has X and Y, The cgsize structure has width and height, so size also has width and height.    (Just curious why cgpoint does not use origin for point)//So access to the four data inside the frame is NSLog (@ "%.0f", view1.frame.origin.x);    NSLog (@ "%.0f", VIEW1.FRAME.ORIGIN.Y);    NSLog (@ "%.0f", view1.frame.size.width);    NSLog (@ "%.0f", view1.frame.size.width);    Of course, we can also get the center of the View1, the center, is the coordinates, including x and Y//in the instantiation of a structure does not need to add *, only the instantiation of an object needs to add * cgpoint view1point=view1.center;    NSLog (@ "%.0f,%.0f", VIEW1POINT.X,VIEW1POINT.Y);    We can also get the border property, is a cgrect, and frame is the difference between//border of the four values inside x and Y is always 0, we can only through this property to get width and height cgrect view1bounds=view1.bounds; NSLOG (@ "%.0f,%.0f,%.0f,%.0f", View1bounds.origin.x,view1bounds.origin.y,view1bounds.size.width,    View1Bounds.size.height);    Set the label, the tag is very useful, such as in the view of multiple views, it is necessary to identify which view1.tag=1;        Set the content mode, this in Uiimageview explained "iOS Development-9", and the image to do a demonstration part of the zoom effect more obvious easy to understand View1.contentmode=uiviewcontentmodecenter;    View1.backgroundcolor=[uicolor Redcolor];        [Self.view Addsubview:view1];    Add a view UIView *view2=[[uiview Alloc]init];    View2.frame=cgrectmake (10, 10, 40, 40);    View2.backgroundcolor=[uicolor Greencolor];        We add the View2 as a sub-view to the View1, at which point the X and Y coordinate positions set above are relative to the view1, that is, the position of the parent view [View1 ADDSUBVIEW:VIEW2];    Add one more view UIView *view3=[[uiview Alloc]init];    View3.frame=cgrectmake (50, 50, 40, 40);    View3.backgroundcolor=[uicolor Bluecolor];        [View1 ADDSUBVIEW:VIEW3];    At this point, the parent view of both View2 and VIEW3 is View1, and you can try to change the parent view background color//A view can have only 1 parent views UIView *supview1=view2.superview;        Supview1.backgroundcolor=[uicolor Blackcolor]; A view can have more than one child view, so the child view is a full array of views. Look, there's a s in the back of the Subviews, and superThere is no S//using for loop to change all sub-view backgrounds to white Nsarray *subview1=view1.subviews;    For (UIView *manyview1 in subView1) {Manyview1.backgroundcolor=[uicolor Whitecolor];    }//Since the Child view property Subviews is an array, then who is in 0, who is in 1, what is the order?    Conclusion: According to the sub-view loading order 0,1,2 ...    Experiment: Take the No. 0 of the sub-view array to change the background color found to be view2, is our first add to View1 in the UIView *whichview1=[subview1 objectatindex:0];        Whichview1.backgroundcolor=[uicolor Redcolor];    Let's create a view4 and load it into Self.view UIView *view4=[[uiview Alloc]init];    View4.frame=cgrectmake (10, 180, 100, 100);    View4.backgroundcolor=[uicolor Purplecolor];    Automatic clipping on, sub-view if there is more than view4 is clipped, the default is no View4.clipstobounds=yes;        [Self.view ADDSUBVIEW:VIEW4];    We create a view5, loaded into the VIEW4//result is view5 too large, beyond the View4 range, if you want to trim out of the outside, you need to set the View4 UIView *view5=[[uiview in the parent view, Alloc]init];    View5.frame=cgrectmake (10, 10, 200, 200);    View5.backgroundcolor=[uicolor Orangecolor];    We can set the transparency, the closer the value is 1, the more opaque, the closer the 0 the more transparent view5.alpha=0.6;        [View4 ADDSUBVIEW:VIEW5]; Let's create two more views, a child view, a parent view,Take a look at the layout of the child view//How does the child view change if the parent view changes?    (1) First set the parent view to allow the sub-view changes, (2) then set the sub-view automatic layout way//Because we have to use a timer to change the size of the parent view, so set it as a global variable, here as long as the initialization can view6=[[uiview Alloc]init];    View6.frame=cgrectmake (30, 300, 100, 100);    View6.backgroundcolor=[uicolor Blackcolor];    (1) The handle view automatic adjustment is set to YES View6.autoresizessubviews=yes;    [Self.view ADDSUBVIEW:VIEW6];    UIView *view7=[[uiview Alloc]init];    View7.frame=cgrectmake (10, 30, 80, 40);    View7.backgroundcolor=[uicolor Whitecolor]; Set the sub-view auto layout mode, there are many, can be used | To use multiple//uiviewautoresizingflexiblewidth-at the same time as the parent view changes the width//uiviewautoresizingflexibleheight-as the parent view changes height//uiviewautoresi Zingflexibleheight | uiviewautoresizingflexiblewidth-both the height and the width are changed, which corresponds to the same proportional scaling as the parent view//uiviewautoresizingflexiblebottommargin-the following margin changes, So the above and left margin does not move//uiviewautoresizingflexiblerightmargin-left, top margin does not move//uiviewautoresizingflexibleleftmargin-right, The margin does not move//uiviewautoresizingflexibletopmargin-left and bottom margin do not move//uiviewautoresizingflexibletopmargin | Uiviewautoresizingflexiblebottommargin-Up/down Center//uiviewautoresizingflexibleleftmargin | uiviewautoresizingflexiblerightmargin-Center//uiviewautoresizingflexibleleftmargin | Uiviewautoresizingflexiblerightmargin | Uiviewautoresizingflexiblebottommargin | uiviewautoresizingflexibletopmargin-equivalent to always keep the ratio of the upper and lower left and right margin, the above adjustment margin of several, their own size is not changed view7.autoresizingmask= Uiviewautoresizingflexibleleftmargin | Uiviewautoresizingflexiblerightmargin | Uiviewautoresizingflexiblebottommargin |    Uiviewautoresizingflexibletopmargin;    [View6 ADDSUBVIEW:VIEW7]; Set a timer to constantly increase the size of the parent view view6, otherwise we cannot see the effect of the sub-view adjusting the layout continuously [Nstimer scheduledtimerwithtimeinterval:0.5 target:self selector:@        Selector (AUTORESIZE1) Userinfo:nil Repeats:yes];    UIView *view8=[[uiview Alloc]init];    UIView *view9=[[uiview Alloc]init];    View8.frame=cgrectmake (30, 450, 100, 100);    View9.frame=cgrectmake (30, 450, 80, 150);    View8.backgroundcolor=[uicolor Blackcolor];    View9.backgroundcolor=[uicolor Redcolor]; Since VIEW8 is loaded first, VIEW9 is loaded, so the post-load view9 is displayed on the topCover View8 [Self.view addsubview:view8];    [Self.view ADDSUBVIEW:VIEW9]; We can manipulate the relationship between the view layers, put the view above or below, but this is naturally done by the parent view, the parent view of View8 and View9 is dead Self.view//Put a sub-view to the bottom [Self.view sendsubviewtoback    : View9];    Put a sub-view on top [Self.view BRINGSUBVIEWTOFRONT:VIEW9];    Action with parent view: Inserts a view in the specified order, this order affects the overlay, the order is more forward in the following UIView *view10=[[uiview Alloc]init];    View10.frame=cgrectmake (30, 450, 120, 60);    View10.backgroundcolor=[uicolor Greencolor];    Because we have inserted a lot of views in this self.view, so the order of View8 and View9 is 5 and 6, so VIEW10 is inserted in 6, then between them [Self.view INSERTSUBVIEW:VIEW10 atindex:6];    We can also specify who to insert under, under VIEW8, that is at the bottom of the [Self.view INSERTSUBVIEW:VIEW10 belowsubview:view8];    We can also specify who to insert on top of the VIEW9, that is on the top of [Self.view INSERTSUBVIEW:VIEW10 ABOVESUBVIEW:VIEW9];        We can also swap the positions of two views, such as switching between 5 and 7, i.e. View8 and VIEW10 [Self.view exchangesubviewatindex:5 withsubviewatindex:7]; After we change the upper and lower layers of the view layer, we actually have 0, 1, 2 in the sub-view array. The allocation order also changes synchronously, this is known when using Exchangesubviewatindex, because it is directly the order of the array of exchanges, you can check Nsarray by the following methods: Subview2=self.view.subviewS    UIView *view11=[subview2 Objectatindex:5];    View11.backgroundcolor=[uicolor Purplecolor];    [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} We want to change the size of the parent view view6 and need to get it, then we need to set it to global variable-(void) autoresize1{View6.frame=cgrectmake (view6.frame.origin.x, VIEW6.FRAME.ORIGIN.Y, view6.frame.size.width+5, view6.frame.size.height+5);} @end

Finally, we must cut a picture to commemorate AH:


"iOS Dev-14" UIView Properties, parent view and child view level actions, child view adaptive mode, plus a timer

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.