UI----1
OC UI (1)一、UILabelUILabel是可以在屏幕上显示文字的一种视图代码演示-(void)creatUI{//定义并创建UILabel对象//UILabel是可以显示在屏幕可以显示文字的UI视图UILabel*label[[UILabel alloc]init];//显示文字label.text同地不同时;label.textColor[UIColor blackColor];//设定label位置label.frameCGRectMake(100,100,200,100);//设置背景颜色label.backgroundColor[UIColor greenColor];//self.view.backgroundColor [UIColor greenColor];label.font[UIFont systemFontOfSize:20];//将label显示到屏幕[self.view addSubview:label];//设置阴影的颜色label.shadowColor[UIColor redColor];label.shadowOffsetCGSizeMake(3,1);//设置文字对齐,默认靠左对齐label.textAlignmentNSTextAlignmentCenter;//自动换行,默认为10为自动换行label.numberOfLines0;}-(void)viewDidLoad{[superviewDidLoad];[selfcreatUI];}二、UIButton这是一个图片按钮创建普通按钮-(void)createUIRectButton{//创建一个btn对象//圆角类型UIButton*btn[UIButton buttonWithType:UIButtonTypeRoundedRect];//设置位置btn.frameCGRectMake(100,100,100,40);//按钮文字内容与状态[btn setTitle:按钮01forState:UIControlStateNormal];[btn setTitle:按钮按下forState:UIControlStateHighlighted];//背景颜色btn.backgroundColor[UIColor greenColor];//设置文字颜色//p1:颜色//p2:状态[btn setTitleColor:[UIColor yellowColor]forState:UIControlStateNormal];//设置风格颜色,也是改变文字颜色优先级弱于上边[btn setTintColor:[UIColor blackColor]];//设置字体大小btn.titleLabel.font[UIFont systemFontOfSize:18];//添加到视图中并显示[self.view addSubview:btn];}创建一个自定义按钮-(void)creatImageBtn{//创建一个自定义类型的btnUIButton*btnImage[UIButton buttonWithType:UIButtonTypeCustom];btnImage.frameCGRectMake(100,200,300,300);UIImage*btn1[UIImage imageNamed:btn01.jpg];UIImage*btn2[UIImage imageNamed:btn02.jpg];//图片名要确保一致[btnImage setImage:btn1 forState:UIControlStateHighlighted];//按下后的图片[btnImage setImage:btn2 forState:UIControlStateNormal];//初始图片[self.view addSubview:btnImage];}自定义按钮的图片要拖进Assets文件UIButton事件处理-(void)creatBt{UIButton*btn[UIButton buttonWithType:UIButtonTypeRoundedRect];btn.frameCGRectMake(100,100,80,40);[btn setTitle:按钮forState:UIControlStateNormal];btn.backgroundColor[UIColor greenColor];//向按钮添加事件函数//p1:谁来实现事件函数实现对象就是谁p2:当按钮满足p3事件类型调用函数p3:事件处理函数类型//UIControlEventTouchUpInside:手指离开屏幕时手指在的位置在按钮范围内触发事件函数[btn addTarget:selfaction:selector(pressBtn:)forControlEvents:UIControlEventTouchUpInside];UIButton*btn2[UIButton buttonWithType:UIButtonTypeRoundedRect];btn2.frameCGRectMake(200,200,80,40);[btn2 setTitle:按钮2forState:UIControlStateNormal];btn2.backgroundColor[UIColor redColor];[btn2 addTarget:selfaction:selector(pressBtn:)forControlEvents:UIControlEventTouchUpInside];btn.tag101;btn2.tag102;[self.view addSubview:btn];[self.view addSubview:btn2];}-(void)pressBtn:(UIButton*)btn{if(btn.tag101){NSLog(btn按钮被按下);}if(btn.tag102){NSLog(btn2按钮被按下);}}-(void)viewDidLoad{[superviewDidLoad];[selfcreatBt];// Do any additional setup after loading the view.}三、UIViewUIView基础-(void)viewDidLoad{[superviewDidLoad];UIView*view[[UIView alloc]init];view.frameCGRectMake(100,100,100,200);view.backgroundColor[UIColor orangeColor];//将新建的视图添加到父视图上[self.view addSubview:view];//是否隐藏视图对象//YES不显示//NO显示view.hiddenNO;//设置视图透明度//alpha 1不透明 0 透明0.5半透明view.alpha1;self.view.backgroundColor[UIColor blueColor];//设置是否不透明//NO透明//YES不透明view.opaqueNO;//将自己从父亲视图删除掉//1:从父亲视图的管理中删除//2:不会显示在屏幕//[view removeFromSuperview];}UIView层级关系-(void)viewDidLoad{[superviewDidLoad];UIView*view1[[UIView alloc]init];view1.frameCGRectMake(100,100,150,150);view1.backgroundColor[UIColor blueColor];UIView*view2[[UIView alloc]init];view2.frameCGRectMake(125,125,150,150);view2.backgroundColor[UIColor greenColor];UIView*view3[[UIView alloc]init];view3.frameCGRectMake(150,150,150,150);view3.backgroundColor[UIColor redColor];[self.view addSubview:view1];[self.view addSubview:view2];[self.view addSubview:view3];[self.view bringSubviewToFront:view1];//将view1移动到最上层[self.view sendSubviewToBack:view3];//将view3移动到最下层//subviews管理所有self.view的子视图的数组UIView*viewFrontself.view.subviews[2];UIView*viewBackself.view.subviews[0];if(viewBackview3){NSLog();}else{NSLog(不相等);}}值得一提的是bringSubviewToFront方法时移动了视图在数组中的位置四、UIWindowUIWindow 是所有视图的顶级容器承载整个 App 界面-(void)scene:(UIScene*)scene willConnectToSession:(UISceneSession*)session options:(UISceneConnectionOptions*)connectionOptions{self.window.rootViewController[[UIViewController alloc]init];//创建根视图控制器self.window.backgroundColor[UIColor blueColor];UIView*view[[UIView alloc]initWithFrame:CGRectMake(100,100,150,150)];view.backgroundColor[UIColor orangeColor];UIView*backview[[UIView alloc]initWithFrame:CGRectMake(200,120,240,360)];backview.backgroundColor[UIColor redColor];//子视图的坐标是参照父亲视图的坐标系//当父亲视图移动的时候所有的子视图会相对移动[backview addSubview:view];[self.window addSubview:backview];[self.window makeKeyAndVisible];//显示根视图NSLog(%\n, %\n, %\n,view.window,backview.window,self.window);}五、UIViewController一个控制器 一个独立页面APP 里的每一页基本都对应一个UIViewController。常用生命周期方法执行顺序init/initWithCoder:控制器初始化只走一次viewDidLoad视图加载完成后调用只走一次用来初始化 UI、网络请求、设置数据viewWillAppear:页面即将显示时调用每次出现都会走viewDidAppear:页面已经显示viewWillDisappear:页面即将消失viewDidDisappear:页面已经消失//// View2.m// UIViewController//// Created by luzi on 2026/4/2.//#importView2.hinterfaceView2()endimplementationView2-(void)touchesBegan:(NSSetUITouch**)touches withEvent:(UIEvent*)event{//使当前的控制器消失掉[selfdismissViewControllerAnimated:YES completion:nil];}-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor[UIColor orangeColor];}-(void)viewWillDisappear:(BOOL)animated{NSLog(% 视图即将消失,[selfclass]);}-(void)viewDidDisappear:(BOOL)animated{NSLog(% 视图已消失,[selfclass]);}-(void)viewDidAppear:(BOOL)animated{NSLog(% 视图已显示,[selfclass]);}-(void)viewWillAppear:(BOOL)animated{NSLog(% 视图即将显示,[selfclass]);}/* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */end//// ViewController.m// UIViewController//// Created by luzi on 2026/4/2.//#importViewController.h#importView2.hinterfaceViewController()endimplementationViewController-(void)touchesBegan:(NSSetUITouch**)touches withEvent:(UIEvent*)event{View2*vc[[View2 alloc]init];[selfpresentViewController:vc animated:YES completion:nil];}//第一次加载视图时调用-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor[UIColor greenColor];NSLog(viewDidLoad第一次加载视图);}//视图控制器的视图即将显示时参数是否用动画切换-(void)viewWillAppear:(BOOL)animated{NSLog(视图即将显示);}//视图即将消失//当前状态视图还是显示在屏幕上-(void)viewWillDisappear:(BOOL)animated{NSLog(视图即将消失);}//-(void)viewDidAppear:(BOOL)animated{NSLog(视图已经出现);}-(void)viewDidDisappear:(BOOL)animated{NSLog(视图已经消失);}end