// .h 中定义
UIButton *_loginBtn;
@property (strong,nonatomic)UIButton *loginBtn;
// .m 中实现设置按钮
@synthesize loginBtn = _loginBtn;//使用备份变量名
UIButton创建 不能使用 UIButton alloc,因为这样UIButton的Style是无法设置的。
所以UIButton只有一种创建方法
[UIButtonbuttonWithType:UIButtonTypeRoundedRect]
//设置按钮的 形状
self.loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
/*
buttonWithType: 定义button按钮的外形
六种定义button类型: 下面有图解
UIButtonTypeCustom = 0, 无类型
UIButtonTypeRoundedRect, 四个角是圆弧 型的
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
*/
//定义button按钮在frame上的坐标(位置),和这个按钮的宽/高
self.loginBtn.frame = CGRectMake(40, 200, 80, 30);
[self.loginBtn setTitle:@"Login" forState:UIControlStateNormal];
/*
常用的属性:
setTitle: 设置button按钮的名称
setImage: [UIImage imageNamed:@"图名"] 添加图片
setTitleColor:[UIColor redColor] 设置字体颜色
forState 设置 按钮点击前后的状态 : 下有图解
UIControlStateHighlighted
UIControlStateSelected
UIControlStateDisabled
UIControlStateNormal
*/
//在视图控制器里面放了一个按钮,想要的效果是选中按钮后,按钮背景图片变成另外一张图,代码如下
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.highlighted = NO;
button.frame = CGRectMake(100, 100, 83, 31);
[button setBackgroundImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"1"] forState:UIControlStateSelected];
[self.view addSubview:button];
// 为按钮添加一个动作
// action: 如果点击的话执行的方法
[self.loginBtn addTarget:self action:@selector(Login:) forControlEvents:UIControlEventTouchUpInside];
//把button控件添加到view中显示
[self.view addSubview:self.loginBtn];
[cpp] view plaincopy
//执行动作的方法
-(IBAction)Login:(id)sender;
/ /Button 点击第一次是一个动作 第二次是另一个动作
- (IBAction)btnAction:(id)sender
{
static BOOL flag=TRUE;
if(flag)
{
[sysView stopAnimating];
[myView stopAnimating];
}
else
{
[sysViewstartAnimating];
[myView startAnimating];
}
NSLog(@"animation isrunning?--%d",[myView isAnimating]);
flag=!flag;
}
// button的tag
-(IBAction)buttonClick:(id)sender //sender参数,表示接受哪个按钮消息
{
UIButton *button = (UIButton *)sender; //参数id是一个通用内型,此处将其强制转换成UIButton内型
//每个button都有唯一的tag,系统默认陪标示用的,是一个整数
NSString *title =[NSString stringWithFormat:@"Buttontag %d",button.tag];//将button tag转换成字符串输出
NSString *mesage = [button currentTitle]; //取得button名称
}
六种定义button类型:
UIButtonTypeCustom = 0, 无类型
UIButtonTypeRoundedRect, 四个角是圆弧 型的
UIButtonTypeDetailDisclosure
UIButtonTypeInfoLight
UIButtonTypeInfoDark
UIButtonTypeContactAdd
forState 设置 按钮点击前后的状态
点击前 点击后
UIControlStateHighlighted
UIControlStateSelected
UIControlStateDisabled
UIControlStateNormal
UIButtonTypeRoundedRect 设置为这个属性,是可以满足我们普通情况下的按钮圆角,当我们在button上添加背景图片和背景颜色的时候就会发现,这个属性并不适用,因为现在的button已经不是圆角的了,它显示的是图片的形状,当设置背景颜色设置为UIButtonTypeCustom属性才可以显示出来。所以我们需要用UIButton控件的其它属性来满足我们的需求
[cpp] view plaincopy
UIButton *btn;
[btn.layer setMasksToBounds:YES];
[btn.layer setCornerRadius:10.0];//设置矩形四个圆角半径 // 如果button设置圆形,正方形,rasius设置为L/2;
/*
[btn.layer setBorderWidth:1.0];//边框宽度
*/
// 设置 button 上的 文字 左对齐
- // button.titleLabel.textAlignment = NSTextAlignmentLeft; 这句无效
- button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
- button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 这行代码,把按钮的内容(控件)
的对齐方式修改为水平左对齐,但是这们会紧紧靠着左边,不好看,
所以我们还可以修改属性:
button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
这行代码可以让按钮的内容(控件)距离左边10个像素,这样就好看多了