UIButton

//UIButton
//    //button提供了通过便利构造器创建的方法
//    UIButton *button = [UIButton buttonWithType: UIButtonTypeSystem];
//    //设置尺寸
//    button.frame=CGRectMake(100, 100, 150, 50);
//    button.backgroundColor = [UIColor grayColor];
//    [self.window addSubview:button];
//    //通过这种方式创建,不需要写release
//    
//    //添加弧度和边框
//    button.layer.cornerRadius = 10;
//    button.layer.borderWidth=1;
//    
//    //给button设置标题
//    [button setTitle:@"确定" forState:UIControlStateNormal];
//    //标题颜色,如果是system,默认是蓝色
//    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//    
//    //设置标题字体大小
//    button.titleLabel.font = [UIFont systemFontOfSize:25];
//    //button的点击方法
//    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

-(void)click:(UIButton *)button{
    //这个参数button就是触发方法对应的那个对象
//    NSLog(@"测试一下");
//    NSLog(@"%@",button.currentTitle);//当前标题
    //点击button更换标题
    if ([button.currentTitle isEqualToString:@"确定"]) {
        [button setTitle:@"登陆" forState:UIControlStateNormal];
    }

}


    UIButton *but1 = [UIButton buttonWithType:UIButtonTypeSystem];
    but1.frame = CGRectMake(100, 100, 150, 50);
    [self.window addSubview:but1];
    but1.layer.borderWidth = 1;
    but1.layer.cornerRadius = 10;
    //设置背景图片
//    [but1 setBackgroundImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];

    [but1 setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];

//点击换图片
    UIButton *but2 = [UIButton buttonWithType:UIButtonTypeCustom];
    but2.frame = CGRectMake(100, 300, 150, 50);
    but2.layer.borderWidth = 1;
    but2.layer.cornerRadius = 10;
    [self.window addSubview:but2];
    [but2 setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];
    [but2 addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
//    [but2 setTitle:@"你好" forState:UIControlStateNormal];
//    [but2 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    //custom是自定义的button,跟system没什么太大区别,但是在设置图片上有区别,一个是setBackGroundImage和setImage有区别,一个就是custom设置标题还需要额外设置标题颜色

-(void)changeImage:(UIButton *)button{
    NSLog(@"%d",button.selected);
    if (button.selected == NO) {
        [button setImage:[UIImage imageNamed:@"BtnOn.png"] forState:UIControlStateNormal];

    }
    else {
        [button setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];

    }
    //对属性进行取反,修改状态
    button.selected = !button.selected;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值