UIButton:按钮
//在创建button的时候,需要指定Button的类型
self.button = [UIButton buttonWithType:UIButtonTypeSystem];
self.button.backgroundColor = [UIColor orangeColor];
self.button.frame = CGRectMake(50, 50, 100, 50);
//设置标题、背景图...必须用此方法,按钮有不同状态
//设置不同状态下的按钮的标题
[self.button setTitle:@"登录" forState:UIControlStateNormal];
self.button.center = self.view.center;
//titleLabel 属性只读是指针不能改变,里面属性可以改 如字体大小
//字体不分状态
self.button.titleLabel.font = [UIFont systemFontOfSize:19.0f];
//获取当前状态下button的title
NSString *str = button.currentTitle;
设置圆角
//设置button圆角、角度 默认为 NO
self.button.layer.masksToBounds = YES;
self.button.layer.cornerRadius = 25;
添加响应事件
方法后面的 “:”,表示有参数(参数一般为本身),也可以不写冒号(没有参数)。
[self.button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];</span>
//实现响应事件方法
- (void)action:(UIButton *)button {
//方法里面写要实现的内容
NSLog(@"button被点击了");
//移除button的响应事件
[self.button removeTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
}
为button设置背景图片
设置背景图后,button的文字也可以显示,背景图片根据button大小铺满button
//UIImage
UIImage *image = [UIImage imageNamed:@"1.png"];
//给button设置背景图片
[self.button setBackgroundImage:image forState:UIControlStateNormal];
//下面这种方法设置,button只显示图片的一部分
[self.button setImage:image forState:UIControlStateNormal];