调整原生UIButton中title和image位置仅需调用此方法:
/**
* 重新布局UIbutton
*
* @param button 所布局的UIbutton
*/
-(void)layoutBtnWithBtn:(UIButton *)button{
button.titleLabel.backgroundColor = button.backgroundColor;
button.imageView.backgroundColor = button.backgroundColor;
CGSize titleSize = button.titleLabel.bounds.size;
CGSize imageSize = button.imageView.bounds.size;
NSLog(@"titleSize :%@ \n imageSize:%@",NSStringFromCGSize(titleSize),NSStringFromCGSize(imageSize));
//文字左移
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width, 0.0, imageSize.width);
//图片右移
button.imageEdgeInsets = UIEdgeInsetsMake(0.0, titleSize.width, 0.0, -titleSize.width);
}
在设置UIbutton title和image的情况下,系统会自动计算文本框和图片的Frame,从而调整两者位置.
在UIbutton 调整两者位置的文章中,发现这位大神讲解的又细致又清晰,附上链接给需要的人:http://www.it610.com/article/5196532.htm