iOS UIButton左文字右图片,上图片下文字

UIButton同设置文字和图片时默认是左图片右边文字,如果需要左文字右图片或者上图片下文字就要设置他们的偏移量,方法如下:

设置完文字图片之后加以下代码:

左文字右图片:

[btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn.imageView.frame.size.width, 0, btn.imageView.frame.size.width)];
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, btn.titleLabel.bounds.size.width, 0, - btn.titleLabel.bounds.size.width)];



上图片下文字:
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示  
[btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height ,-btn.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度,距离左边框减少imageView的宽度,距离下边框和右边框距离不变  
[btn setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0,0.0, -btn.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度,其它不边  
 
posted @ 2019-12-18 17:10  落花伊人  阅读(1231)  评论(0编辑  收藏  举报