1.UIWindow
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOption{
//UIWindow
//启动程序完成后,创建window
self.window = [UIWindow alloc]initWithFrame:[[UIScreen mainScreen].bounds];
//给window设置背景色
self.window.backgroundColor = [UIColor whiteColor];
//指定KeyWindow,并设置可见
[self.window makeKeyAndVisible];}
//UI的窗口类, 用于显示界面, 一般一个应用至少要有一个window
//iOS中的空间都是矩形, 决定一个空间位置及大小, 用结构体CGRect
//CGPoint, 结构体, 用来存储x, y轴坐标
CGPoint point = CGPointMake(10, 20);
NSLog(@"%.2f, %.2f", point.x, point.y);
NSLog(@"%@", NSStringFromCGPoint(point));
//CGSize, 结构体, 用于存储宽度和高度
CGSize size = CGSizeMake(200, 300);
NSLog(@"%.2f, %.2f", size.width, size.height);
NSLog(@"%@", NSStringFromCGSize(size));
//CGRect, 结构体, 用于存储矩形的位置和大小
CGRect rect = CGRectMake(10, 20, 200, 300);
NSLog(@"%.2f, %.2f, %.2f, %.2f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
NSLog(@"%@", NSStringFromCGRect(rect));
//[UIScreen mainScreen], 获取当前手机屏幕
//[[UIScreen mainScreen] bounds], 获取当前手机屏幕大小
NSLog(@"%@", NSStringFromCGRect([[UIScreen mainScreen] bounds]));
//设置窗口背景色
self.window.backgroundColor = [UIColor whiteColor];
//设置为主窗口并显示
[self.window makeKeyAndVisible];
2.UIView
//UIView是UI中的视图类, 代表屏幕上的⼀个矩形区域, 在应用中显示的全部都是UIView或者是它的子类, 不同的控件代表不同种类的view
//创建一个UIView 在MRC模式下, 需要手动释放
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 40, 50, 100)];
[self.window addSubview:view];//添加视图(添加到视图数组中)
view.backgroundColor = [UIColor redColor];//修改颜色
[view release];//数组会保留一份, 所以可以直接release
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(285, 40, 50, 100)];
view2.backgroundColor = [UIColor redColor];
[self.window addSubview:view2];
[view2 release];
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(30, 527, 50, 100)];
view3.backgroundColor = [UIColor redColor];
[self.window addSubview:view3];
[view3 release];
UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(285, 527, 50, 100)];
view4.backgroundColor = [UIColor redColor];
[self.window addSubview:view4];
[view4 release];
NSLog(@"%@", self.window.subviews);
从父视图上移除视图
//(1)一个一个分别移除
[view removeFromSuperview];
[view2 removeFromSuperview];
[view3 removeFromSuperview];
[view4 removeFromSuperview];
//(2)一次性全部移除
for (UIView *tempView in self.window.subviews) {
[tempView removeFromSuperview];
}
NSLog(@"%@", self.window.subviews);
//父视图
NSLog(@"%@", view.superview);
//一个视图, 可以有多个子视图
//子视图数组
NSLog(@"%@", view.subviews);
//添加一个视图的步骤
//1.创建视图
//2.设置视图属性(背景颜色)
//3.添加的父视图
//4.释放内存
//获取视图的位置和大小
NSLog(@"%@", NSStringFromCGRect(self.window.frame));
//假设有红色视图、绿色视图和蓝色视图
//创建红色视图
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
redView.backgroundColor = [UIColor redColor];
[self.window addSubview:redView];
redView.bounds = CGRectMake(10, 0, redView.bounds.size.width, redView.bounds.size.height);
//当一个view设置bounds时,会把自己当成一个容器,定义自己的界面的大小以及左上角的初始 坐标当子视图添加到次试图时,会根据bounds指定的原点(0 ,0)计算frame,而非左上角。改变bounds改变的是子视图的位置
//创建绿色视图
UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
greenView.backgroundColor = [UIColor greenColor];
[self.window addSubview:greenView];
//红色视图提最前 bringSubviewToFront: 放最后:sendSubviewToBack:: 交换
交交换exchangeSubviewAtIndex:3 withSubviewAtIndex:1
[self.window bringSubviewToFront:redView];
//创建蓝色视图
UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(80, 80, 100, 100)];
//添加子视图到某个位置
blueView.backgroundColor = [UIColor blueColor];
[self.window insertSubview:blueView atIndex:1];//添加蓝色视图到某个位置
[self.window insertSubview:blueView aboveSubview:greenView];//添加蓝色视图到绿色视图上面
[self.window insertSubview:blueView belowSubview:redView];//添加蓝色视图到红色视图下面
[blueView release】;
3.UIColor
//颜色类
//UIView不设置背景色, 默认为clearColor
//自定义颜色
UIColor *customColor = [UIColor colorWithRed:100/255. green:100/255. blue:30/255. alpha:1.0];
self.window.backgroundColor = customColor;
//创建一个随机颜色的视图
for (NSInteger i = 0; i < 12; i++) {
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(i*25, i*25, 100, 100)];
UIColor *tempColor = [UIColor colorWithRed:arc4random() *256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:0.56];
tempView.backgroundColor = tempColor;
[self.window addSubview:tempView];
[tempView release];
}
for (UIView *tempView in self.window.subviews) {
[tempView removeFromSuperview];
}
4.UIView的属性值
UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
//设置背景色
centerView.backgroundColor = [UIColor cyanColor];
//设置是否隐藏
centerView.hidden = NO;
//设置透明度
centerView.alpha = 0.5;
//设置frame
centerView.frame = CGRectMake(0, 188, 200, 200);
//注:一个视图的frame只能够整体修改
//如果想要单个修改, 必须新建一个对象才能完成
CGRect tempRect = centerView.frame;
tempRect.size.width = 300;
centerView.frame = tempRect;
//frame的坐标系是父视图的坐标系
//设置中心点
//(1)
centerView.center = CGPointMake(self.window.frame.size.width / 2.0, self.window.frame.size.height / 2.0);
//(2)
centerView.center = self.window.center;
[self.window addSubview:centerView];
[centerView release];
//中心点的坐标系是父视图的坐标系
//设置bounds, 它是这个视图在自身坐标系的位置和大小
NSLog(@"%@", NSStringFromCGRect(centerView.bounds));
centerView.bounds = CGRectMake(0, 0, 300, 300);
UIView *aView= [[UIView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
aView.backgroundColor = [UIColor redColor];
[centerView addSubview:aView];
[aView release];
//frame, center, bounds
//1.frame修改, 会影响到center和bounds
//2.center修改, 会影响frame, bounds不发生变化
//3.bounds修改, 会影响到frame和center
1.UILabel
// 标签视图, 主要用于显示文字
//UILabel继承于UIView
//(1)创建
//状态栏20, 20
UILabel *userLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 335, 40)];
//(2)设置属性
userLabel.backgroundColor = [UIColor grayColor];
//设置文本
userLabel.text = @"登陆";
//设置字体样式
//系统字体(和加粗二选一)
userLabel.font = [UIFont systemFontOfSize:25];
//系统字体加粗
userLabel.font = [UIFont boldSystemFontOfSize:40];
//自定义文字样式
userLabel.font = [UIFont fontWithName:@"" size:40];
//设置文本颜色
userLabel.textColor = [UIColor blackColor];
//设置行数, 默认是1, 0代表无限行
userLabel.numberOfLines = 0;
//设置文字对齐方式
userLabel.textAlignment = NSTextAlignmentCenter;
//设置文字阴影
userLabel.shadowColor = [UIColor grayColor];
//设置阴影偏移
userLabel.shadowOffset = CGSizeMake(2, 2);
//文本大小适应label宽度
userLabel.adjustsFontSizeToFitWidth = YES;
//字体大小, 缩放比率
userLabel.minimumScaleFactor = 0.5;
//(3)添加父视图
[self.window addSubview:userLabel];
//(4)释放
[userLabel release];
2.UITextField
//UITextFiled, 文本输入框, 用于输入和显示文字
//(1)创建
UITextField *userTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 80, 335, 150)];
//(2)设置属性
userTextField.backgroundColor = [UIColor grayColor];
//设置输入框的文本
userTextField.text = @"请输入用户名";
//设置占位符
userTextField.placeholder = @"请输入用户名";
//设置文本颜色
userTextField.textColor = [UIColor redColor];
//设置文字样式
userTextField.font = [UIFont systemFontOfSize:50];
//获得支持的所有字体的类型
NSLog(@“%@“,[UIFont familyNames]);
//设置对齐方式
userTextField.textAlignment = NSTextAlignmentCenter;
//设置输入框边框样式(和背景图无法同时存在)
userTextField.borderStyle = UITextBorderStyleRoundedRect;
//输入框背景图
userTextField.background = [UIImage imageNamed:@“11.png"];
//是否允许用户输入
userTextField.enabled = YES;
//开始编辑文本时, 清空内容(和清空按钮二选一)
userTextField.clearsOnBeginEditing = YES;
//设置清空按钮
userTextField.clearButtonMode = UITextFieldViewModeWhileEditing/Always;
//设置输入安全
userTextField.secureTextEntry = YES;
//设置弹出键盘样式
userTextField.keyboardType = UIKeyboardTypeDefault;
//设置return按钮样式
userTextField.returnKeyType = UIReturnKeyDone;
//设置代理
userTextField.delegate = self;
//输入视图
passWord1.inputView = view1; //软键盘消失,呈现view1
//辅助视图
passWord1.inputAccessoryView = view1;
passWord1.text = @"瞅你咋的";
//自定义键盘
//UIView *keyboardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 280)];
//userTextField.inputView = keyboardView;
//[keyboardView release];
//设置leftView
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
leftView.backgroundColor = [UIColor redColor];
userTextField.leftView = leftView;
[leftView release];
//设置leftView显示模式
userTextField.leftViewMode = UITextFieldViewModeAlways;
//(3)添加到父视图
[self.window addSubview:userTextField];
//4.释放
[userTextField release];
UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
centerView.center = self.window.center;
centerView.backgroundColor = [UIColor colorWithRed:0.800 green:1.000 blue:0.400 alpha:1.000];
//设置边框
centerView.layer.borderWidth = 10;
centerView.layer.borderColor = [UIColor greenColor].CGColor;
//设置圆角弧度
centerView.layer.cornerRadius = 50;
//设置整体阴影
//设置阴影偏移
centerView.layer.shadowOffset = CGSizeMake(10, 10);
//设置阴影颜色, 默认为黑
centerView.layer.shadowColor = [UIColor blackColor].CGColor;
//设置阴影不透明度
centerView.layer.shadowOpacity = 1.0;
//设置超出边界部分是否显示
centerView.clipsToBounds = YES;
[self.window addSubview:centerView];
[centerView release];
3.UIButton
//UIButton, 按钮类, 用于响应和处理一些事件
//(1)创建
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//UIButtonTypeDetailDisclosure 详细信息(浅色背景)
//UIButtonTypeInfoDark 详细信息(深色背景)
//UIButtonTypeInfoLight 详细信息(浅色背景)
//UIButtonTypeSystem 系统样式
//UIButtonTypeContactAdd 加号按钮
//UIButtonTypeCustom 自定义格式,需要添加图片的时候需要使用此种类型
//(2)设置属性
button.frame = CGRectMake(20, 240, 335, 40);
button.backgroundColor = [UIColor grayColor];
//设置文字
[button setTitle:@"你点我一下绝对不会怀孕的" forState:UIControlStateNormal];
[button setTitle:@"高亮状态" forState:UIControlStateHighlighted];
[button setTitle:@"选中状态" forState:UIControlStateSelected];
[button setTitle:@"不可用状态" forState:UIControlStateDisabled];
//设置字体大小
button.titleLabel.font = [UIFont systemFontOfSize:30];
//设置文字颜色
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//设置图片
[button setImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
//设置背景图片
[button setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
//控件状态
//UIControlStateNormal: 正常状态
//UIControlStateHighlighted: 高亮状态
//UIControlStateSelected:选中状态
//UIControlStateDisabled: 不可用状态
//关联事件(方法)
//参数1: 目标, 谁来调用方法
//参数2: 事件, 关联的方法名
//参数3: 触发事件
//添加点击事件
[button addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(pressButtonWithAlertView) forControlEvents:UIControlEventTouchUpInside];
//接口文件遵守<UITextFieldDelegate,UIAlertViewDelegate>代理协议,声明属性的实例变量,是与其目标建立联系
//声明一个点击方法
-(void)didClickLoginButton
{
NSLog(@"%s %d",__FUNCTION__,__LINE__);
//回收键盘 resignFirstResponser
[self.usernameField resignFirstResponder];
[self.passwordFiled resignFirstResponder];
//UIAlertview 警告
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"你输入的密码有误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
[alert show];
}
//点击return回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"点击return键%s %D",__FUNCTION__,__LINE__);
if (self.usernameField == textField) {
[self.passwordFiled becomeFirstResponder];
}elseif (self.passwordFiled == textField){
[textField resignFirstResponder];
}
returnYES;
}
#pragma mark-----点击屏幕空白处回收键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.userField resignFirstResponder];
}
}
//按照响应事件移除
[button removeTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
//设置按钮是否选中
button.selected = YES;
//设置按钮是否可用
button.enabled = NO;
//(3)添加到父视图
[self.window addSubview:button];
//(4)千万不要释放
4.UIAlertView
- (void)pressButtonWithAlertView {
// 警告框
//(1)创建
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您有一百万元到账!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//(2)显示
[alertView show];
//(3)释放
[alertView release];
}
5.程序启动流程
#import "ApplicationDelegate.h"
@implementation ApplicationDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//应用程序运行期间,只会运行一次
NSLog(@"应用启动,载入完成%s %d",__FUNCTION__,__LINE__);
returnYES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
//按HOME进入后台。将要离开激活状态
NSLog(@"将要离开激活状态,%s %d",__FUNCTION__,__LINE__);
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
//进入后台调用的方法
NSLog(@"应用已经进入后台,%s %d",__FUNCTION__,__LINE__);
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
//应用将要进入前台
NSLog(@"应用将要进入前台,%s %d",__FUNCTION__,__LINE__);
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
//应用进入激活状态
NSLog(@"应用进入激活状态%s %d",__FUNCTION__,__LINE__);
}
- (void)applicationWillTerminate:(UIApplication *)application {
//应用将要结束
NSLog(@"应用将要结束%s %d",__FUNCTION__,__LINE__);
}
@end
1.自定义label—textField视图(复合设计模式)
//创建LTView继承子UIView
#import <UIKit/UIKit.h>
@interface LTView : UIView
//左侧的Label
@property(strong,nonatomic) UILabel *leftLabel;// strong == retain
//右侧的UITextField
@property(strong,nonatomic) UITextField *rightField;
//初始化接口
-(instancetype)initWithFrame:(CGRect)frame title:(NSString *)title;
//设置占位符的方法
-(void)setPlaceHolderString:(NSString *)string;
@end
#import "LTView.h"
@implementation LTView
//利用initWithFrame ,去初始化leftlabel和 rightField
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0, 80,30)];
[self addSubview:_leftLabel];
_rightField = [[UITextField alloc]initWithFrame:CGRectMake(80,0, frame.size.width - 80 ,30)];
[self addSubview:_rightField];
_rightField.borderStyle = UITextBorderStyleRoundedRect;
}
returnself;
}
//构造器初始化接口
-(instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
{
self = [self initWithFrame:frame];
if (self) {
_leftLabel.text = title;
}
returnself;
}
//设置占位符的方法
-(void)setPlaceHolderString:(NSString *)string
{
_rightField.placeholder = string;
}
//重写Frame
-(void)setFrame:(CGRect)frame{
[super setFrame:frame];
_rightField.frame = CGRectMake(80,0, frame.size.width-80,30);
}
@end