Manual Layout Sub-view of IOS development _ios

Source: Internet
Author: User
Tags uikit

manually layout the child view;
Let's look at the effect chart below, the effect we are going to achieve today:


Here we default to start with storyboard:
First we're going to create a parent view on the white screen Superview (blue background), create four smaller views in the parent view (orange background)
Look at the code below,
Inside the SuperView.h file:

#import <UIKit/UIKit.h> @interface superview:uiview{uiview * _VIEW01;
 UIView * _VIEW02;
 UIView * _VIEW03;

UIView * _VIEW04;

}//Declare CREATE VIEW function-(void) createsubviews; @end in the superview.m file: #import "SuperView.h" @interface Superview () @end @implementation Superview-(void) creates
 ubviews{//upper left corner view _view01 = [[UIView alloc] init];

 _view01.frame=cgrectmake (0, 0, 40, 40);
 upper right corner view _view02 = [[UIView alloc] init];

 _view02.frame=cgrectmake (self.bounds.size.width-40, 0, 40, 40);
 Lower right corner view _view03 = [[UIView alloc] init];

 _view03.frame=cgrectmake (self.bounds.size.width-40, self.bounds.size.height-40, 40, 40);
 Lower left corner View _view04 = [[UIView alloc] init];

 _view04.frame=cgrectmake (0, self.bounds.size.height-40, 40, 40);
 _view01.backgroundcolor=[uicolor Orangecolor];
 _view02.backgroundcolor=[uicolor Orangecolor];
 _view03.backgroundcolor=[uicolor Orangecolor];

 _view04.backgroundcolor=[uicolor Orangecolor];
 [Self addsubview:_view01];
 [Self addsubview:_view02];[Self addsubview:_view03];

[Self addsubview:_view04];
 ////Adjust the position of the child view manually by calling this function//when a redistribution is required//(void) layoutsubviews{[UIView Beginanimations:nil Context:nil];

 [UIView Setanimationduration:1];
 _view01.frame=cgrectmake (0, 0, 40, 40);
 _view02.frame=cgrectmake (self.bounds.size.width-40, 0, 40, 40);
 _view03.frame=cgrectmake (self.bounds.size.width-40, self.bounds.size.height-40, 40, 40);

 _view04.frame=cgrectmake (0, self.bounds.size.height-40, 40, 40);

[UIView commitanimations];
 } @end

Inside the viewcontroller.m file:

#import "ViewController.h" #import "SuperView.h" @interface Viewcontroller () @end @implementation Viewcontroller-(
 void) Viewdidload {[Super viewdidload];

 Do no additional setup after loading the view, typically from a nib.
 Create a parent view Superview * Sview = [[Superview alloc]init];

 Sview.frame = CGRectMake (20, 20, 180, 280);

 The parent view calls the function to create four trivial views [Sview createsubviews];
 Sview.backgroundcolor = [Uicolor Bluecolor];

 [Self.view Addsubview:sview];
 UIButton * Btn01 = [UIButton buttonwithtype:uibuttontyperoundedrect];
 Btn01.frame = CGRectMake (240, 480, 80, 40);
 [Btn01 settitle:@ "enlarge" forstate:uicontrolstatenormal];
 [Btn01 addtarget:self Action: @selector (Presslarge) forcontrolevents:uicontroleventtouchupinside];

 [Self.view Addsubview:btn01];
 UIButton * btn02 = [UIButton buttonwithtype:uibuttontyperoundedrect];
 Btn02.frame = CGRectMake (240, 520, 80, 40);
 [Btn02 settitle:@ "shrink" forstate:uicontrolstatenormal]; [Btn02 addtarget:self Action: @selector (Presssmall) ForcontrolevEnts:uicontroleventtouchupinside];

 [Self.view ADDSUBVIEW:BTN02];


Sview.tag = 101;
 }//Enlarge parent View-(void) presslarge{Superview * Sview = (superview*) [Self.view viewwithtag:101];
 [UIView Beginanimations:nil Context:nil];
 [UIView Setanimationduration:1];
 Sview.frame=cgrectmake (20, 20, 280, 400);

[UIView commitanimations];
 }//Reduce parent View-(void) presssmall{Superview * Sview = (superview*) [Self.view viewwithtag:101];
 [UIView Beginanimations:nil Context:nil];
 [UIView Setanimationduration:1];
 Sview.frame=cgrectmake (20, 20, 180, 280);
[UIView commitanimations];
 }-(void) didreceivememorywarning {[Super didreceivememorywarning];
Dispose of any of the can is recreated.
 } @end

  above code writing finished, reached the above view effect, I hope to help you learn, but also hope that we support the cloud-dwelling community.

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.