参考地址:
https://www.cnblogs.com/xujiahui/p/6909317.html
UIButton * button =[[UIButton alloc] initWithFrame:CGRectMake(30, 200, 300, 50)];
button.backgroundColor =[UIColor grayColor];
//图片
[button setImage:[UIImage imageNamed:@"but"] forState:UIControlStateNormal];
//文字
[button setTitle:@" But文字" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font =[UIFont boldSystemFontOfSize:40];
[self.view addSubview:button];
UIButton * button =[[UIButton alloc] initWithFrame:CGRectMake(30, 200, 300, 50)];
button.backgroundColor =[UIColor grayColor];
//文字
[button setTitle:@"But文字" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font =[UIFont boldSystemFontOfSize:40];
//图片
[button setImage:[UIImage imageNamed:@"but"] forState:UIControlStateNormal];
/修改///
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
/*
UIControlContentHorizontalAlignmentCente
UIControlContentHorizontalAlignmentLeft
UIControlContentHorizontalAlignmentRight
UIControlContentHorizontalAlignmentFill */
//文字 可以显示在 button 视图之外,但不接收点击事件响应
[button setTitleEdgeInsets:UIEdgeInsetsMake(button.imageView.frame.size.height ,button.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度,距离左边框减少imageView的宽度,距离下边框和右边框距离不变
///
[self.view addSubview:button];
UIButton * button =[[UIButton alloc] initWithFrame:CGRectMake(30, 200, 300, 50)];
button.backgroundColor =[UIColor grayColor];
//文字
[button setTitle:@"But文字" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font =[UIFont boldSystemFontOfSize:40];
//图片
[button setImage:[UIImage imageNamed:@"xia"] forState:UIControlStateNormal];
/修改///
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.imageEdgeInsets = UIEdgeInsetsMake(0,300-50, 0, 0); //上左下右
//button.titleEdgeInsets = UIEdgeInsetsMake(0,300-50, 0, 0); // 设置文字的位置 上左下右
///
[self.view addSubview:button];
4/ 但是图片尺寸大了 位置会错乱, 如有遇到的请评论指点