UIButton setImage 和 setTitle之后,默认的 image和title 对齐方式是居中,
由于 title 长度不固定,所以如果要几个这样有image有title的按钮纵向排列对齐,无论你怎么调整 imageEdgeInsets和titleEdgeInsets,都有可能导致前面图片对得不整齐,
设置如下:
btn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
UIButton更多的对齐方式
typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) {
UIControlContentVerticalAlignmentCenter = 0, //竖直方向,居中对齐
UIControlContentVerticalAlignmentTop = 1, //竖直方向,居上对齐
UIControlContentVerticalAlignmentBottom = 2, //竖直方向,居底对齐
UIControlContentVerticalAlignmentFill = 3, //竖直方向,填充
};
typedef NS_ENUM(NSInteger, UIControlContentHorizontalAlignment) {
UIControlContentHorizontalAlignmentCenter = 0, //左右方向,居中对齐
UIControlContentHorizontalAlignmentLeft = 1, //左右方向,居左对齐
UIControlContentHorizontalAlignmentRight = 2, //左右方向,居右对齐
UIControlContentHorizontalAlignmentFill = 3, //左右方向,填充
};