// 一、AppDelegate协议 应用程序启动完毕
// 应用程序启动完毕
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1.创建一个应用程序窗口, 跟屏幕一样大小
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//2.设置背景颜色
self.window.backgroundColor = [UIColor whiteColor];
//3.让self.window成为主屏幕并且可见
[self.window makeKeyAndVisible];
//4.设置视图控制器(视图的载体)RootViewController
self.window.rootViewController = [[RootViewController alloc]init];
return YES;
}
// 二、UIView
// (1) 基本创建:
// 视图控制器的view
self.view.backgroundColor = [UIColor whiteColor];
// 1.创建一个view(视图), 指定其位置和大小;
// frame 包括两个结构体: origin, size
// CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
// x,y 距离 屏幕 左上角的间距; width, height 宽 和 高
UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 150, 150)];
// 2.设置背景颜色, 默认没有颜色
redView.backgroundColor = [UIColor blueColor];
// 3.将 view 添加到 父视图 上面(父视图 和 子视图)
[self.view addSubview:redView];//将view贴上
UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(150, 150, 100, 100)];
blueView.backgroundColor = [UIColor redColor];
// 4.view 也可以做父视图
[self.view addSubview:blueView];
// [redView addSubview:blueView];
// (2) 视图本身的操作:
// frame是相对于父视图而言的
// x, y : 自己的左上角的坐标点 相对于 父视图 左上角的坐标点 的距离
// 1.frame(CGRect) view 的一个属性
blueView.frame = CGRectMake(100, 100, 150, 150);
// 2.center(CGPoint)更改blueView的中心点: 视图自身的中心 到 父视图左上角的距离
blueView.center = CGPointMake(100, 100);
// 3.bounds(CGRect) x, y没有用, 中心点不变
blueView.bounds = CGRectMake(200, 3000, 50, 50);
// (3) 父视图 和 子视图
// 1.父视图对子视图的操作 (拿到前面, 下标为最大值; 后面为最小值: 0)
[self.view bringSubviewToFront:redView];//将一个子视图放到最前面
[self.view sendSubviewToBack:redView];//将一个子视图放到最后面
// 2.子视图从父视图移除
[redView removeFromSuperview];
// 3.获取一个视图的子视图(返回值是数组)
NSArray *subviews = [self.view subviews];
NSLog(@"子视图的个数: %ld", subviews.count);
UIView *subview1 = subviews[0];
subview1.backgroundColor = [UIColor blackColor];
// 4.根据下标调整子视图的位置(上下顺序)
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:2];
// 5.获取一个子视图的父视图
UIView *superview = [redView superview];
superview.backgroundColor = [UIColor orangeColor];
// (4) View 的操作:
// 1.通过RGB以及透明度设置颜色, 范围0 - 1
superview.backgroundColor = [UIColor colorWithRed:1 green:231 / 255 blue:96 / 255 alpha:0.7];
// 2.是否隐藏, 默认是NO, 影响子视图
blueView.hidden = YES;
// 3.透明度 0 ~ 1(完全不透明), 影响子视图
redView.alpha = 0.3;
// 4.tag标记, 方便获取被标记的视图(在别的方法中调用view)
redView.tag = 10;
[self test];//调用者是本视图的父视图
UI - UIView、UILabel、AppDelegate
最新推荐文章于 2024-05-05 00:31:44 发布