Manual layout sub-view;
Let's look at the results we're going to achieve today:
Here we start by default with storyboard:
First we'll create a parent view on the white screen Superview (blue background), create four small 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;}//声明创建视图函数-(void) createSubViews;@end
Inside the superview.m file:
#import "SuperView.h" @interface superview ()@end @implementation superview -(void) createsubviews{//upper left corner view_VIEW01 = [[UIViewALLOC] init]; _view01. Frame=cgrectmake (0,0, +, +);//upper right corner view_VIEW02 = [[UIViewALLOC] init]; _view02. Frame=cgrectmake ( Self. Bounds. Size. Width- +,0, +, +);//Lower right corner view_view03 = [[UIViewALLOC] init]; _view03. Frame=cgrectmake ( Self. Bounds. Size. Width- +, Self. Bounds. Size. Height- +, +, +);//lower left corner view_VIEW04 = [[UIViewALLOC] init]; _view04. Frame=cgrectmake (0, Self. Bounds. Size. Height- +, +, +); _view01. BackgroundColor=[UicolorOrangecolor]; _view02. BackgroundColor=[UicolorOrangecolor]; _view03. BackgroundColor=[UicolorOrangecolor]; _view04. BackgroundColor=[UicolorOrangecolor]; [ SelfADDSUBVIEW:_VIEW01]; [ SelfADDSUBVIEW:_VIEW02]; [ SelfADDSUBVIEW:_VIEW03]; [ SelfADDSUBVIEW:_VIEW04];}//Call this function when a re-layout is required//re-set the location of the child view through this function//Manually adjust the position of the child view-(void) layoutsubviews{[UIViewBeginanimations:NilContextNil]; [UIViewSetanimationduration:1]; _view01. Frame=cgrectmake (0,0, +, +); _view02. Frame=cgrectmake ( Self. Bounds. Size. Width- +,0, +, +); _view03. Frame=cgrectmake ( Self. Bounds. Size. Width- +, Self. Bounds. Size. Height- +, +, +); _view04. Frame=cgrectmake (0, Self. Bounds. Size. Height- +, +, +); [UIViewCommitanimations];}@end
Inside the viewcontroller.m file:
#import "ViewController.h" #import "SuperView.h" @interface viewcontroller ()@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];additional setup after loading the view, typically from a nib. //Create a parent viewSuperview * Sview = [[Superview alloc]init]; Sview. Frame= CGRectMake ( -, -, the,280);//Parent View Call function to create four small views[Sview Createsubviews]; Sview. BackgroundColor= [UicolorBluecolor]; [ Self. ViewAddsubview:sview];UIButton* Btn01 = [UIButtonButtonwithtype:uibuttontyperoundedrect]; Btn01. Frame= CGRectMake ( -,480, the, +); [Btn01 settitle:@"Zoom In"Forstate:uicontrolstatenormal]; [Btn01 AddTarget: SelfAction@selector(Presslarge) forcontrolevents:uicontroleventtouchupinside]; [ Self. ViewADDSUBVIEW:BTN01];UIButton* BTN02 = [UIButtonButtonwithtype:uibuttontyperoundedrect]; Btn02. Frame= CGRectMake ( -,520, the, +); [Btn02 settitle:@"Zoom Out"Forstate:uicontrolstatenormal]; [Btn02 AddTarget: SelfAction@selector(Presssmall) forcontrolevents:uicontroleventtouchupinside]; [ Self. ViewADDSUBVIEW:BTN02]; Sview. Tag=101;}//Enlarge parent View-(void) presslarge{Superview * Sview = (superview*) [ Self. ViewViewwithtag:101]; [UIViewBeginanimations:NilContextNil]; [UIViewSetanimationduration:1]; Sview. Frame=cgrectmake ( -, -,280, -); [UIViewCommitanimations];}//Shrink Parent View-(void) presssmall{Superview * Sview = (superview*) [ Self. ViewViewwithtag:101]; [UIViewBeginanimations:NilContextNil]; [UIViewSetanimationduration:1]; Sview. Frame=cgrectmake ( -, -, the,280); [UIViewCommitanimations];} - (void) Didreceivememorywarning {[SuperDidreceivememorywarning];//Dispose of any resources, can be recreated.}@end
The above code is finished, the above view has reached the effect, the code is not commented too much, because the previous code inside the comment is very detailed, now not one by one comments, specific can see my previous article, thank you!
iOS development from getting started to mastering--manual layout sub-view