UIview lianxi

时间:2023-03-08 21:30:20

// 创建一个和屏幕大小相同的window,记住[UIScreen mainScreen].bounds 是获取当前屏幕大小

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// 设置window的背景颜色

self.window.backgroundColor = [UIColor grayColor];

// 将window设置上,并让window显示

[self.window makeKeyAndVisible];

/*

// 打印屏幕大小,使用NSStringFromCGRect()

NSLog(@"%@", NSStringFromCGRect([UIScreen mainScreen].bounds));

// 添加一小块视图到屏幕上

// 1. 申请空间,并初始化大小

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];

// 2. 设置view相关属性

view.backgroundColor = [UIColor whiteColor];

view.bounds = CGRectMake(20, 0, 200, 200);

// 3. 添加到window上

// 添加view到_window上,给_window发送消息

// 将一个view添加到window上时,view的引用计数会+1

[_window addSubview:view];

// 4. 发送一次release消息

[view release];

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];

view2.backgroundColor = [UIColor redColor];

[view addSubview:view2];

[view2 release];

// 使用中心点更改view的位置

//    view.center = CGPointMake(320 / 2, 568 / 2);

//    NSLog(@"%@", NSStringFromCGPoint(view.center));

//    view2.center = CGPointMake(view.frame.size.width / 2, view.frame.size.height / 2);

//    NSLog(@"%@", NSStringFromCGPoint(view2.center));

*/

//view1 蓝色

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];

view1.backgroundColor = [UIColor blueColor];

view1.tag = 1000;

[_window addSubview:view1];

[_window viewWithTag:1000].backgroundColor = [UIColor yellowColor];//根据tag值,取出view 设置

[view1 release];

//view2 红色

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(80, 20, 100, 400)];

view2.backgroundColor = [UIColor redColor];

view2.tag = 1001;

[_window addSubview:view2];

//[_window insertSubview:view2 belowSubview:view1];//将红 插到 蓝的底下

[view2 release];

//view3 紫色

UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];

view3.backgroundColor = [UIColor purpleColor];

view3.tag = 1002;

view3.hidden = NO;

view3.center = CGPointMake(160, 160);//设置中心点属性

[_window addSubview:view3];

//[_window insertSubview:view3 atIndex:1];

[view3 release];

// 将view提到最上层

//[_window bringSubviewToFront:view2];

// 将view放到最下层

// [_window sendSubviewToBack:view2];

// 将view2从父视图上移除

//[view2 removeFromSuperview];

NSLog(@"%@", view1.superview);

NSLog(@"%@", _window.subviews);

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 350, 200, 40)];

label.text = @"cai bi";

label.font = [UIFont fontWithName:@"Helvetica-Bold" size:40];

[_window addSubview:label];

[label release];

//

[_window viewWithTag:1000].alpha = 0.5;