UIButton的常用设置及响应事件的添加

1.创建对象
1) UIButton *Button = [UIButton buttonWithType:UIButtonTypeSystem];
2) Button.frame = CGRectMake(100, 200, 150, 50);
3) Button.backgroundColor = [UIColor colorWithRed:0.931 green:0.531 blue:0.133 alpha:1.000];
4) [self.view addSubview:Button];

2.添加文字
[Button setTitle:@”你点我” forState:UIControlStateNormal];
[Button setTitle:@”你还真点啊” forState:UIControlStateHighlighted];//长按是字符变成它;

3.设置圆角
Button.layer.masksToBounds = YES;
Button.layer.cornerRadius = 20;

4.设置按钮边框颜色
Button.layer.borderWidth = 1;
Button.layer.borderColor = [UIColor blackColor].CGColor;

5.在按钮里添加图片
UIImage *image = [UIImage imageNamed:@”1.png”];
[Button setBackgroundImage:image forState:UIControlStateNormal];

6.更改按钮里的字体
button2.titleLabel.font = [UIFont systemFontOfSize:20 weight:20];

7.移动按钮里文字的位置
self.button4.titleEdgeInsets = UIEdgeInsetsMake(50, -200, 60, 100);

8设置按钮中文字的颜色
[ Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

9 .给按钮添加响应事件
1)添加响应
- (void)viewDidLoad {
[Button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];//在范围内松开有效,点击滑出再松开,无效.

}
2)实现响应

  • (void)action:(UIButton *)sender{
    NSLog(@“点我作甚”);
    }

  • (void)action:(UIButton *)sender{
    [sender removeTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];//使响应只响应一次

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值