iOS UIButton之UIEdgeInsets詳解

級別:★★☆☆☆
標簽:「UIButton內偏移量」「titleEdgeInsets」「imageEdgeInsets」
作者: MrLiuQ
審校: QiShare團隊

我們先看一下蘋果官方對UIEdgeInsets說明:

typedef struct UIEdgeInsets {
    CGFloat top, left, bottom, right;  
// specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
} UIEdgeInsets;
  • 官方:specify amount to inset (positive) for each of the edges. values can be negative to 'outset' .
  • 解釋:對每條邊向內方向的偏移量,可以為正值(向內偏移)也可以為負值(向外偏移)。

基本使用:

xxx.edgeInsets = UIEdgeInsetsMake(.0, .0, .0, .0);

//例如我們設置UICollectionView的edgeInset會使collectionView產生內偏移
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(20.0, .0, .0, .0);

引入正題:
然而,UIButton的內偏移量與其他控件有些區別,
因為UIButton內部默認有兩個子控件:UILabelUIImageView
所以UIButton在內偏移量的計算上會有差異。
為什么?先賣個關子,下文解釋。

UIButton默認子控件位置

UIButtonEdgeInsets:

需求:通過修改edgeInsets,改變Button內部的imageView和titleLabel的相對位置。

思路:通過修改button的兩個屬性:titleEdgeInsetsimageEdgeInsets,從而達到最終的具體需求。

這里列出了三個比較常見的需求:

  • image左 title右
  • image右 title左
  • image上 title下

小編做了一個demo,效果如下圖:

Demo效果圖

場景一:左圖右字

button.titleEdgeInsets = UIEdgeInsetsMake(0, 10.0, 0, 0);

語法解釋:設置了title的左邊線的內偏移量為10.0,

但經測試,
注意:實際上title和Image只有5.0的距離
注意:實際上title和Image只有5.0的距離
注意:實際上title和Image只有5.0的距離

圖解如下:

在這種場景下:

  • title的 上邊線、右邊線、下邊線 內偏移 是相對于contentView的
  • image的 上邊線、左邊線、下邊線 內偏移 是相對于contentView的
  • title的 左邊線 內偏移 相對于image
  • image的 右邊線 內偏移 相對于title

場景二:左字右圖

button.titleEdgeInsets = UIEdgeInsetsMake(.0, - button.imageView.bounds.size.width - 10.0, .0, button.imageView.bounds.size.width);
button.imageEdgeInsets = UIEdgeInsetsMake(.0, button.titleLabel.bounds.size.width, .0, - button.titleLabel.bounds.size.width);

語法解釋:

  • title的 左邊線 內偏移 - (imageView.width +10)<=> 等價于 title的左邊線 向 內偏移的反方向 移動 (image.width +10)
  • title的 右邊線 內偏移 imageView.width <=> 等價于 title的右邊線 向 內偏移的正方向 移動 imageView.width
  • image的 左邊線 內偏移 titleLabel.width <=> 等價于 image的左邊線 向 內偏移的正方向 移動 titleLabel.width
  • image的 右邊線 內偏移 - titleLabel.width <=> 等價于 title的左邊線 向 內偏移的反方向 移動 titleLabel.width

是不是有點繞人?哈哈,不要慌,請看圖解

場景三:上圖下字

button.titleEdgeInsets = UIEdgeInsetsMake(button.imageView.frame.size.height + 10.0, - button.imageView.bounds.size.width, .0, .0);
button.imageEdgeInsets = UIEdgeInsetsMake(.0, button.titleLabel.bounds.size.width / 2, button.titleLabel.frame.size.height + 10.0, - button.titleLabel.bounds.size.width / 2);

語法解釋:

  • title的 上邊線 內偏移 (imageView.height +10)<=> 等價于 title的上邊線 向 內偏移的正方向 移動 (image.height +10)
  • title的 左邊線 內偏移 - imageView.width <=> 等價于 title的左邊線 向 內偏移的反方向 移動 image.width
  • image的 左邊線 內偏移 titleLabel.width * 0.5 <=> 等價于 image的左邊線 向 內偏移的正方向 移動 titleLabel.width 的一半
  • image的 下邊線 內偏移 titleLabel.height + 10 <=> 等價于 image的下邊線 向 內偏移的正方向 移動 titleLabel.height + 10
  • image的 右邊線 內偏移 - titleLabel.width * 0.5 <=> 等價于 image的右邊線 向 內偏移的反方向 移動 titleLabel.width 的一半

請看圖解:

最后,本文Demo鏈接:GitHub


關注我們的途徑有:
QiShare(簡書)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公眾號)

推薦文章:iOS UISlider數值與滑塊聯動

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容