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

iOS学习(1)——UIButton

2015-06-02 10:55 453 查看
//创建自己需要的类型的按钮
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

//设置按钮的fram
[button setFrame:CGRectMake(0, 0, 50, 50)];
button.frame=CGRectMake(0, 0, 50, 50);
CGRect rect=button.frame;

//设置按钮的tag,用来标识
[button setTag:1];
button.tag=1;
NSInteger tag=button.tag;

//设置按钮背景颜色,在storyboard里面没有这个属性,只能在代码里设置
[button setBackgroundColor:[UIColor greenColor]];
button.backgroundColor=[UIColor greenColor];

//设置按钮是否隐藏
[button setHidden:YES];
button.hidden=NO;
BOOL hidden=button.hidden;

//设置按钮文字的布局
[button setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];
button.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
//这种方法不起作用
button.titleLabel.textAlignment=NSTextAlignmentRight;

//设置按钮文字
[button setTitle:@"点我" forState:UIControlStateNormal];
NSString *titleText=button.titleLabel.text;
//这种方法无效
//button.titleLabel.text=@"点我";

//设置按钮文字大小
button.titleLabel.font=[UIFont systemFontOfSize:14];
//已经被废弃
[button setFont:[UIFont systemFontOfSize:14]];

//设置按钮文字颜色
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//这种方法无效
//button.titleLabel.textColor=[UIColor whiteColor];

//设置文字距离边框的间距
button.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
//设置按钮图片
[button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
button.imageView.image=[UIImage imageNamed:@"icon_customer_mine_intrest.png"];

//设置按钮事件
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];

//添加到父控件里
[self.view addSubview:button];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息