1)UIView视图frame的设置,四个参数,前2个确定位置,后2个确定大小。
(2)UIView的内容模式contentMode,和在UIImage中说的是一样的,而且在UIImage中展示更容易理解。
(3)UIView最重要的就是父视图和子视图之间的关系,以及父视图操作子视图(增加一个子视图,把一个子视图放到最下面最上面、交换两个子视图的加载顺序等等)
(4)还有一个重要的是,父视图如果发生变化,子视图怎么自动调整布局:先让父视图允许子视图干这个事,即把父视图的属性autoresizesSubviews设置YES,然后再对对应的要自动调整布局的子视图设置某一种自适应模式(有很多种模式,如变动自身宽和高来保证距离上、下、左、右不变或者保持自身高宽不变来变动距离上下左右边距)。
(5)这里还用到了一个定时器。这是在OC里面讲解到的,当做是复习。
- #import "ViewController.h"
-
- @interface ViewController ()
-
- @end
-
- @implementation ViewController
- {
- UIView *view6;
- }
-
- - (void)viewDidLoad {
-
- UIView *view1=[[UIView alloc]init];
-
-
- view1.frame=CGRectMake(10, 30, 100, 100);
-
-
- NSLog(@"%.0f",view1.frame.origin.x);
- NSLog(@"%.0f",view1.frame.origin.y);
- NSLog(@"%.0f",view1.frame.size.width);
- NSLog(@"%.0f",view1.frame.size.width);
-
-
- CGPoint view1Point=view1.center;
- NSLog(@"%.0f,%.0f",view1Point.x,view1Point.y);
-
-
- CGRect view1Bounds=view1.bounds;
- NSLog(@"%.0f,%.0f,%.0f,%.0f",view1Bounds.origin.x,view1Bounds.origin.y,view1Bounds.size.width,view1Bounds.size.height);
-
- view1.tag=1;
-
- view1.contentMode=UIViewContentModeCenter;
-
- view1.backgroundColor=[UIColor redColor];
- [self.view addSubview:view1];
-
-
- UIView *view2=[[UIView alloc]init];
- view2.frame=CGRectMake(10, 10, 40, 40);
- view2.backgroundColor=[UIColor greenColor];
-
- [view1 addSubview:view2];
-
-
- UIView *view3=[[UIView alloc]init];
- view3.frame=CGRectMake(50, 50, 40, 40);
- view3.backgroundColor=[UIColor blueColor];
- [view1 addSubview:view3];
-
-
-
- UIView *supView1=view2.superview;
- supView1.backgroundColor=[UIColor blackColor];
-
-
-
- NSArray *subView1=view1.subviews;
- for (UIView *manyView1 in subView1) {
- manyView1.backgroundColor=[UIColor whiteColor];
- }
-
-
-
-
- UIView *whichview1=[subView1 objectAtIndex:0];
- whichview1.backgroundColor=[UIColor redColor];
-
-
- UIView *view4=[[UIView alloc]init];
- view4.frame=CGRectMake(10, 180, 100, 100);
- view4.backgroundColor=[UIColor purpleColor];
-
- view4.clipsToBounds=YES;
- [self.view addSubview:view4];
-
-
-
- UIView *view5=[[UIView alloc]init];
- view5.frame=CGRectMake(10, 10, 200, 200);
- view5.backgroundColor=[UIColor orangeColor];
-
- view5.alpha=0.6;
- [view4 addSubview:view5];
-
-
-
-
-
- view6=[[UIView alloc]init];
- view6.frame=CGRectMake(30, 300, 100, 100);
- view6.backgroundColor=[UIColor blackColor];
-
- view6.autoresizesSubviews=YES;
- [self.view addSubview:view6];
- UIView *view7=[[UIView alloc]init];
- view7.frame=CGRectMake(10, 30, 80, 40);
- view7.backgroundColor=[UIColor whiteColor];
-
-
-
-
-
-
-
-
-
-
-
- view7.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
- [view6 addSubview:view7];
-
- [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];
-
- [self.view addSubview:view8];
- [self.view addSubview:view9];
-
-
- [self.view sendSubviewToBack:view9];
-
- [self.view bringSubviewToFront:view9];
-
- UIView *view10=[[UIView alloc]init];
- view10.frame=CGRectMake(30, 450, 120, 60);
- view10.backgroundColor=[UIColor greenColor];
-
- [self.view insertSubview:view10 atIndex:6];
-
- [self.view insertSubview:view10 belowSubview:view8];
-
- [self.view insertSubview:view10 aboveSubview:view9];
-
- [self.view exchangeSubviewAtIndex:5 withSubviewAtIndex:7];
-
-
- NSArray *subView2=self.view.subviews;
- UIView *view11=[subView2 objectAtIndex:5];
- view11.backgroundColor=[UIColor purpleColor];
-
- [super viewDidLoad];
-
- }
-
- -(void)autoResize1{
- view6.frame=CGRectMake(view6.frame.origin.x, view6.frame.origin.y, view6.frame.size.width+5, view6.frame.size.height+5);
- }
-
- @end
最后必须截个图作纪念啊:

[转]UIView的属性,父视图和子视图的层级操作,子视图的自适应模式,外加一个定时器
原文:http://www.cnblogs.com/ianhao/p/4442669.html