UIButton setImageEdgeInsets、setTitleEdgeInsets、setContentEdgeInsets

一:只設(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的位置。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容