iOS UIButton的UIEdgeInsets

他们默认的状态是图片在左,标题在右,而且image和title之间没有空隙;

title和image两个属性的效果是相辅相成的。

 

真相只有一个:
image的UIEdgeInsets属性的top,left,bottom都是相对于按钮的,right是相对于title;
title的UIEdgeInsets属性的top,bottom,right都是相对于按钮的,left是相对于image;

 

title在左,image在右:

//拿到title和image的大小:
CGSize titleSize = self.pureButton.titleLabel.bounds.size;
CGSize imageSize = self.pureButton.imageView.bounds.size;
//分别设置偏移量:记住偏移量是位移
self.pureButton.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, 0, imageSize.width);
self.pureButton.imageEdgeInsets = UIEdgeInsetsMake(0, titleSize.width, 0, -titleSize.width);

  

image在上,title在下:

//图片 向右移动的距离是标题宽度的一半,向上移动的距离是图片高度的一半
//标题 向左移动的距离是图片宽度的一半,向下移动的距离是标题高度的一半

self.pureButton.imageEdgeInsets = UIEdgeInsetsMake(-imageSize.height/2, titleSize.width/2, imageSize.height/2, -titleSize.width/2);
self.pureButton.titleEdgeInsets = UIEdgeInsetsMake(titleSize.height/2, -imageSize.width/2, -titleSize.height/2, imageSize.width/2);

  

 

 

posted on 2017-09-22 11:30  iRemark  阅读(251)  评论(0编辑  收藏  举报

导航