关于按钮的一些基本的用法
UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(40, 40, 60, 100)];
//按钮背景颜色
button.backgroundColor = [UIColor orangeColor];
//按钮title
[button setTitle:@"button" forState:UIControlStateNormal];
//按钮title字体颜色
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//按钮圆角
button.layer.cornerRadius = 10.0;
//按钮的点击事件
[button addTarget:self action:@selector(clickB) forControlEvents:UIControlEventTouchUpInside];
//按钮中title的位置设置
button.titleEdgeInsets = UIEdgeInsetsMake(60, 0, 10, 0);
//点击和非点击状态按钮的背景 高亮和非高亮状态
[button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
[self.view addSubview: button];
文字的位置如下图所示:
有时候按钮上会有图片和文字同时存在的情况,这个时候图片和文字的位置有上下结构的,有左右结构的。
图片和文字开始的位置是左边图片,右边文字,然后设置成上下的布局,是根据他们原先的文字设置他们的 上左下右
[self setImage:[UIImage imageNamed:@"loveBag"] forState:UIControlStateNormal];
self.imageEdgeInsets = UIEdgeInsetsMake(30,16,5,16);
[self setTitle:[dateFormatter stringFromDate:_date] forState:UIControlStateNormal];
self.titleEdgeInsets = UIEdgeInsetsMake(15,-24,20, 0);
按钮上就一个图标 设置图片在按钮上的位置:
[wxbutton setImageEdgeInsets:UIEdgeInsetsMake(10, 5, 10, 5)];
图片和文字的位置:如下例子
UIButton *shareButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0, 30, 40)];
[shareButton setTitle:@"分享" forState:UIControlStateNormal];
[shareButton setTitleColor:[UIColor colorWithHexString:@"7c7c7c"] forState:UIControlStateNormal];
shareButton.titleEdgeInsets = UIEdgeInsetsMake(20, -40, 3, 0);
[shareButton setImage:[UIImage imageNamed:@"h_share"] forState:UIControlStateNormal];
shareButton.imageEdgeInsets = UIEdgeInsetsMake(0,5,20,5);
shareButton.tag = 2;
[shareButton addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];
//(上 ,左 , 下 , 右 )
//20 是图片的高度 宽度20。。文字在图片下方 位置的top为20 -40是按钮的宽度,