UIButton

UIButton的常用方法

//初始化一个button按钮

/*
 UIButtonTypeCustom = 0,                         // no button type
 UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // standard system button

 UIButtonTypeDetailDisclosure,
 UIButtonTypeInfoLight,
 UIButtonTypeInfoDark,
 UIButtonTypeContactAdd,

 UIButtonTypeRoundedRect = UIButtonTypeSystem,

 */
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; //系统样式按钮
button.frame = CGRectMake(100, 200, 200, 50);
button.backgroundColor = [UIColor redColor];
[self.window addSubview:button];

//设置按钮边框颜色和边框大小和倒角

button.layer.borderColor = [UIColor blackColor].CGColor;
button.layer.borderWidth = 5.0f;
button.layer.cornerRadius = 5.0f;

//设置按钮标题

[button setTitle:@"普通状态" forState:UIControlStateNormal];

[button setTitle:@"高亮状态" forState:UIControlStateHighlighted];

// 禁用状态的时候所有其他效果都会失效 无法触发

[button setTitle:@"禁用状态" forState:UIControlStateDisabled];
button.enabled = NO;

[button setTitle:@"选择状态" forState:UIControlStateSelected];

// 设置为选中状态

button.selected = YES;

//设置按钮文字颜色

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];

//添加事件

//参数1:触发方法的对象
//参数2:触发的方法
//参数3:触发方法的方式
//:需要传参数的
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

[button removeTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
//设置tag
button.tag = 100;

//点击事件

- (void)buttonClick:(UIButton *)button
{
//父视图通过tag值获取子视图
UIButton *btn = (UIButton *)[self.window viewWithTag:100];
NSLog(@"title == %@",btn.currentTitle);
}

UIButton test

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor grayColor];
button.frame = CGRectMake(100, 200, 100, 50);
[button setTitle:@"播放" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitle:@"暂停" forState:UIControlStateSelected];
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];

[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
//设置按钮字体大小
button.titleLabel.font = [UIFont systemFontOfSize:24];

//设置按钮文字偏移
//上左下右
button.titleEdgeInsets = UIEdgeInsetsMake(10, 0, 0, 0);

//button.selected = YES 选中状态
//button.selected =  NO  普通状态
[self.window addSubview:button];

return YES;

}

- (void)buttonClick:(UIButton *)button
{
button.selected = !button.selected;
}

UIButton设置图片

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor cyanColor];
button.frame = CGRectMake(100, 200, 100, 50);
[self.window addSubview:button];

//获取图片

//方法1:通过图片名字获取image

//如果图片格式是png格式可以不加后缀(png苹果系统本身支持的格式) 如果是其他格式都是需要添加后缀的
UIImage *image = [UIImage imageNamed:@"001@2x"];
UIImage *backImage = [UIImage imageNamed:@"map"];


//方法2:通过图片路径获取image

UIImage *pathImage = [UIImage imageWithContentsOfFile:@"/Users/liuguilin/Desktop/Image/1.png"];

//方法3:通过工程文件获取image

//加载项目中的文件

NSString *path = [[NSBundle mainBundle] pathForResource:@"4" ofType:@"jpg"];
UIImage *pathImage2 = [UIImage imageWithContentsOfFile:path];

//设置按钮图片

[button setImage:image forState:UIControlStateNormal];

//设置背景图片

[button setBackgroundImage:backImage forState:UIControlStateNormal];

//设置图片偏移

//    button.imageEdgeInsets = UIEdgeInsetsMake(10, 0, 0, 0);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值