Assets.xcassets 资源文件夹
main.m
![](https://img.haomeiwen.com/i13471358/a180bb87e122f90d.png)
第三个参数只是负责各种状态的切换
类文件
类对象(类描述信息,有多少个属性)第一次使用该类创建实例对象,会先创建类对象。
根据类的描述(类对象)信息在堆中开辟内存空间创建实例对象。
一个类在使用时,只有一个类对象,但可以有很多的类的对象(实例)。
AppDelegate.m
@implementation AppDelegate
//应用程序启动时 调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSLog(@"应用程序启动时");
//获取屏幕的边框矩形
CGRect screenFrame = [UIScreen mainScreen].bounds;
//创建 window对象
self.window = [[UIWindow alloc] initWithFrame:screenFrame];
//创建UIViewController 对象
UIViewController *vc =[[UIViewController alloc] init];
//设置视图控制器中的 视图 背景颜色为红色
vc.view.backgroundColor =[UIColor redColor];
//创建标签
UILabel *label = [[UILabel alloc]init];
label.frame = CGRectMake(100, 100, 200, 400);
label.backgroundColor = [UIColor greenColor];
label.text = @"这是一个标签";
label.font = [UIFont systemFontOfSize:30];
//将标签添加到 视图控制器的视图上
[vc.view addSubview:label];
//将创建好的 视图控制器对象 给 window 的根视图控制器属性赋值
self.window.rootViewController = vc;
//将window显示出来
[self.window makeKeyAndVisible];
return YES;
}
//将要进入后台时 调用
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"应用程序将要进入后台");
}
//已经进入后台时 调用
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"应用程序已经进入后台");
}
//将要回到前台时 调用
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"应用程序将回到前台");
}
//已经激活时自动调用
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"应用已经激活时自动");
}
//将要推出时 调用
- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(@"应用将要推出时");
}
//使用的 匿名变量 匿名对象
self.window =[[ UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//创建自定义的控制器对象
MyViewController *mVC = [[MyViewController alloc]init];
//设置window的rootViewController为定义的控制器对象
self.window.rootViewController = mVC;
//将window显示出来
[self.window makeKeyAndVisible];
return YES;
设置控制器自带视图的背景颜色
self.view.backgroundColor = [UIColor yellowColor];
//创建Label对象
UILabel *label =[[UILabel alloc]init];
label.frame = CGRectMake(100, 100, 200, 400);
label.backgroundColor = [UIColor greenColor];
//设置label的显示内容
label.text = @"这是文本的显示内容这是文本的显示内容";
//设置label的字体
label.font = [UIFont systemFontOfSize:36];
//设置label显示的行数 设置为0 换行到结束
label.numberOfLines = 0;
//设置label文本的对齐方式
label.textAlignment =NSTextAlignmentRight;
//设置文本颜色
label.textColor = [UIColor blueColor];
//设置文本的阴影颜色
label.shadowColor =[UIColor redColor];
//设置文本的阴影的偏移量
label.shadowOffset =CGSizeMake(-3, 3);
//将Label添加到self.view上
[self.view addSubview:label];
sizeThatFits 该方法的参数 宽度为实际宽度 高度要尽量大包含所有的文本,该方法的返回值是一个CGSize类型的变量,该size中宽式原来的宽,高是根据label中的文本内容自动计算的高度
//sizeThatFits 不会改变label本身的 frame
CGSize labelSize = [label sizeThatFits:CGSizeMake(200, 999)];
// //根据实际宽高 修改 label 的frame
// CGRect frame = label.frame;
// frame.size = labelSize;
// label.frame = frame;
//sizeToFit 根据label中的文本内容,自己计算高度,并修改label的frame中的高度
[label sizeToFit];
sizeToFit 根据label中的文本内容,自己计算高度,并修改label的frame中的高度
[self.view addSubview:label];
按钮UIButton
点击后有响应
直接父类:UIControl(会把此分支叫控件)
- 特点:四个状态(normal摆在那不动,highLighted点下去不松手,selected,enable)
- 常用属性
frame大小和位置
设置文字:setTitle:forState:
设置图片:setlmage:forState:
调整内边距:
setlmageEdgeInsets
setTitleEdgeInSets - 常用方法
addTarget:方法用于添加点击事件
先创建一个RootViewController(根视图控制器)
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[MyViewController alloc]init];
[self.window makeKeyAndVisible];
网友评论