您的位置:首页 > 产品设计 > UI/UE

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

2015-12-22 15:27 651 查看
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];//使响应只响应一次

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息