07TextField_UIImageView_UIButton_设置圆角

一、文本框TextField

    //文本框 UITextField
    //1.1创建
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(60, 100, 100, 40)];
    //1.2设置背景
    textField.backgroundColor = [UIColor whiteColor];
    //1.3设置样式
    //UITextBorderStyleNone,
    //UITextBorderStyleLine,
    //UITextBorderStyleBezel,
    //UITextBorderStyleRoundedRect
    textField.borderStyle = UITextBorderStyleRoundedRect;
    //1.4设置提示信息
    textField.placeholder = @"请输入账号";
    //1.5设置安全性
    //    textField.secureTextEntry = YES;
    //1.6
    textField.text = @"123456";
    //2.添加到window上
    [self.window addSubview:textField];

二、图片
1.UIImageView控件

    //UIImageView:图片视图,用来展示图片
    //1.1创建
    //初始化方法:
    //a.init b.initWithFrame c.initWithImage
    //1.1.b初始化方法b
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = CGRectMake(20, 20, 280, 280);
    //1.1.c初始化方法c
    
    //1.2背景
    imageView.backgroundColor = [UIColor brownColor];
    //1.3设置Image属性展示具体的图片
    imageView.image = [UIImage imageNamed:@"3.png"];
    
    
    //注意:
    //1.4 UIImageView与UIImage的区别
    //UIImageView : UIView
    //UIImage : NSObject <NSSecureCoding>
    //相框与图片的关系,两者继承者不一样,相互独立,没有直接关系
    //1.5
    //若图片格式为png,可以省略后缀名“.png”,其他格式不可省略。
    
    //2.添加到window
    [self.window addSubview:imageView];

2.1拖拽图片进入Supporting Files

2.2设置属性
勾选1、2、4选项

注意:选项1一定要勾选,跟路径有关,保证当前项目可用,其他项目也可用。

三、UIButton控件
1.UIButton的属性、方法

    //按钮:button
    //1.1创建
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 60, 40)];
    //1.2颜色
    button.backgroundColor = [UIColor brownColor];
    //1.3绑定点击事件
    //1.3.1 Target:目标,对象
    //1.3.2 action:行为,动作
    //1.3.3 forControlEvents:控制事件
    //方法意义:
    //让*做*事情,在*情况下
    //让Target做action,在forControlEvents的情况下
    //即:self调用buttonClick,在UIControlEventTouchUpInside事件发生的时候
    //注意:button的点击事件buttonClick,一定要实现,不然,点击button之后会崩溃
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    //1.4 设置标题
    [button setTitle:@"Login" forState:UIControlStateNormal];
    //1.5 字体颜色
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //1.6 背景图片
//    [button setBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    //[button setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];//设置图片
    //1.7 设置文字
    button.titleLabel.font = [UIFont systemFontOfSize:20];
    
    //1.8 设置圆角
    //a.设置圆角半径
    button.layer.cornerRadius = 30;
    //b.剪切
    button.clipsToBounds = YES;
    //1.8.b 例子
    //1.8.b.1 (10,20):相对于window的坐标
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(10, 20, 100, 100)];
    view1.backgroundColor = [UIColor blueColor];
    [self.window addSubview:view1];
    //1.8.b.2 (10,20):相对于view1的坐标
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 150, 150)];
    imageView.image = [UIImage imageNamed:@"2"];
    [view1 addSubview:imageView];
    //clipsToBounds属性:把超出view1的图片剪切掉
    view1.clipsToBounds = YES;
    
    //1.9设置其他属性,样式,居中,居右
    //可以进入UIButton控件类,看其中的属性与方法
    
    //2.将button放入window
    [self.window addSubview:button];
    return YES;
}
//button点击事件
//它属于入口类APPDelegate的方法
//因此就由入口类的对象(self)来调用
- (void)buttonClick
{
    NSLog(@"Button is clicked!");
}

2.带参数的buttonClick方法

    //带参数的buttonClick事件
    //注意:只能带一个参数
    //参数类型是UIButton类型
    //作用:传递UIButton对象
    UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(40, 40, 60, 40)];
    NSLog(@"button1 = %@",button1);
    [button1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    //设置tag
    button1.tag = 1;
    button1.backgroundColor = [UIColor grayColor];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button1 setTitle:@"变红" forState:UIControlStateNormal];
    [self.window addSubview:button1];
    
    UIButton *button2 = [[UIButton alloc]initWithFrame:CGRectMake(40, 100, 60, 40)];
    NSLog(@"button2 = %@",button2);
    [button2 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    //设置tag
    button2.tag = 2;
    button2.backgroundColor = [UIColor grayColor];
    [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button2 setTitle:@"变绿" forState:UIControlStateNormal];
    [self.window addSubview:button2];
    return YES;
}

//带参数的buttonClick方法
//button:当前所点击的button
- (void)buttonClick:(UIButton *)button
{
    NSLog(@"button = %@",button);
    //方法1:用titleLabel.text属性
//    if ([button.titleLabel.text  isEqual: @"变红"]) {
//        self.window.backgroundColor = [UIColor redColor];
//    }
//    else if ([button.titleLabel.text  isEqual: @"变绿"])
//    {
//        self.window.backgroundColor = [UIColor greenColor];
//    }
    
    //方法2:用tag属性
    if (button.tag == 1)
    {
        self.window.backgroundColor = [UIColor redColor];
    }
    else if (button.tag == 2)
    {
        self.window.backgroundColor = [UIColor greenColor];
    }
}

3.创建button的另一种方式

    //创建button的另外一种方式
    UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
    button.frame = CGRectMake(100, 100, 60, 40);
    button.backgroundColor = [UIColor blueColor];
    [self.window addSubview:button];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容