#在蓝懿学习iOS的日子#Day11
今天主要是学习控件的使用方式
一、UIView
UIView *v = [[UIViewalloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//*****属性
//背景颜色
v.backgroundColor = [UIColor purpleColor];
//透明度
v.alpha = .5;
//隐藏
// v.hidden = YES;
// frame 位置和大小
v.frame = CGRectMake(50, 50, 200, 200);
// 显示边界 大小
v.bounds = CGRectMake(0, 0, 300, 300);
// 中心点
v.center = self.view.center;
//拿到所有的子控件
NSArray *views = self.view.subviews;
//**********方法
//添加控件
// [self.view addSubview:v];
// 插入控件
[self.view insertSubview:v atIndex:0];
// 把某个子控件 带到最前端显示
// [self.view bringSubviewToFront:self.greenView];
//交换子控件的位置
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// 获取父视图
//背景颜色
v.backgroundColor = [UIColor purpleColor];
//透明度
v.alpha = .5;
//隐藏
// v.hidden = YES;
// frame 位置和大小
v.frame = CGRectMake(50, 50, 200, 200);
// 显示边界 大小
v.bounds = CGRectMake(0, 0, 300, 300);
// 中心点
v.center = self.view.center;
//拿到所有的子控件
NSArray *views = self.view.subviews;
//**********方法
//添加控件
// [self.view addSubview:v];
// 插入控件
[self.view insertSubview:v atIndex:0];
// 把某个子控件 带到最前端显示
// [self.view bringSubviewToFront:self.greenView];
//交换子控件的位置
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// 获取父视图
// UIView *superView = v.superview;
- (IBAction)clicked:(id)sender {
//遍历所有的子控件
for (UIView *v in self.view.subviews) {
//判断对象是什么类型
if (![v isMemberOfClass:[UIButton class]]) {
//从界面中删除控件
[v removeFromSuperview];
}
}
//遍历所有的子控件
for (UIView *v in self.view.subviews) {
//判断对象是什么类型
if (![v isMemberOfClass:[UIButton class]]) {
//从界面中删除控件
[v removeFromSuperview];
}
}
}
二、uiLabel
UILabel *l = [[UILabel alloc]initWithFrame:CGRectMake(20, 200, 200, 200)];
[self.view addSubview:l];
l.backgroundColor = [UIColor yellowColor];
l.text = @"abcd";
l.textColor = [UIColor greenColor];
l.font = [UIFont fontWithName:@"zapfino" size:35];
//显示的行数
l.numberOfLines = 0;
[l setTextAlignment:NSTextAlignmentCenter];
//阴影
l.shadowColor = [UIColor redColor];
//阴影显示的位置
[self.view addSubview:l];
l.backgroundColor = [UIColor yellowColor];
l.text = @"abcd";
l.textColor = [UIColor greenColor];
l.font = [UIFont fontWithName:@"zapfino" size:35];
//显示的行数
l.numberOfLines = 0;
[l setTextAlignment:NSTextAlignmentCenter];
//阴影
l.shadowColor = [UIColor redColor];
//阴影显示的位置
l.shadowOffset = CGSizeMake(5, 5);
三、UIbutton
UIButton *btu = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];
// UIButton*btu = [UIButton buttonWithType:UIButtonTypeSystem];
// btu.frame =CGRectMake(100, 200, 100, 50);
//在按钮输入文本 按钮的正常状态
[btu setTitle:@"wo" forState:UIControlStateNormal];
//按钮的点击下去的状态
[btu setTitle:@"hahah" forState:UIControlStateHighlighted];
//按钮的颜色
// UIButton*btu = [UIButton buttonWithType:UIButtonTypeSystem];
// btu.frame =CGRectMake(100, 200, 100, 50);
//在按钮输入文本 按钮的正常状态
[btu setTitle:@"wo" forState:UIControlStateNormal];
//按钮的点击下去的状态
[btu setTitle:@"hahah" forState:UIControlStateHighlighted];
//按钮的颜色
btu.backgroundColor = [UIColor redColor];
//添加事件
[btu addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];
[btu addTarget:self action:@selector(myDownAction:) forControlEvents:UIControlEventTouchDown];
[btu addTarget:self action:@selector(myDownAction:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btu];
-(void)myDownAction:(UIButton*)btn{
// [btn removeFromSuperview];
NSLog(@"按下了");
}
-(void)myAction{
NSLog(@"松手了");
// [btn removeFromSuperview];
NSLog(@"按下了");
}
-(void)myAction{
NSLog(@"松手了");
}