一:只設(shè)置Image
設(shè)置setImageEdgeInsets之后通過Debug View 查看ImageView的frame,得到數(shù)據(jù):(Unbutton的Width都為335,圖片的Size為(32,32))
| ImageView.x | ImageEdgeInsets | x得到的規(guī)則 |
|---|---|---|
| 151.5 | (0, 0, 0, 0) | ( 335 - 32 )/ 2 |
| 156.5 | (0, 10, 0, 0) | (335 - 32 - 10 )/ 2 + 10 |
| 101.5 | (0, 0, 0, 100) | (335 - 32 - 100 )/ 2 + 0 |
| 106.5 | (0, 10, 0, 100) | (335 - 32 - 10 - 100 )/ 2 + 10 |
總結(jié):x的得到規(guī)則 (width-image.width-imageEdgeInset.left-imageEdgeInset.right)/2
二:既設(shè)置Image,又設(shè)置title
| ImageView.x | Label.frame | ImageEdgeInsets | TitleEdgeInsets |
|---|---|---|---|
| 124.5 | (156.5,9.5,54,21.5) | (0,0,0,0) | (0,0,0,0) |
| 149.5 | (156.5,9.5,54,21.5) | (0,50,0,0) | (0,0,0,0) |
| 149.5 | (106.5,9.5,54,21.5) | (0,50,0,0) | (0,0,0,100) |
ImageView.x的計算規(guī)則有一點變化了:(button的寬度 - 離左右的距離 - 圖片的寬度 -Label的寬度)/ 2 + 離左邊的距離。
Label.x 的計算規(guī)則:(button的寬度 - 離左右的距離 - 圖片的寬度 -Label的寬度)/ 2 + 離左邊的距離 + 圖片的寬度。(默認(rèn)左邊總有你設(shè)置的圖片似得)
既有圖片又有title的話,計算居中時總是以兩個的的寬度為一體的。
三:在第二種的基礎(chǔ)上,再設(shè)置setContentEdgeInsets
| ImageView.x | Label.frame | setContentEdgeInsets |
|---|---|---|
| 134.5 | (166.5, 9.5, 54, 21.5) | (0, 20, 0, 0 |
在原有的基礎(chǔ)上相當(dāng)于把button的Width - 離左右邊的距離,之后再計算Image和Label的位置。