iOS:交換Button中圖片與文字的左右位置


titleEdgeInsets屬性和 imageEdgeInsets屬性只是在畫這個button出來的時候用來調整image和label位置的屬性,並不影響button本身的大小。
它們只是image和button相較於原來位置的偏移量。

如果要image在右邊,label在左邊,那image的左邊相對於button的左邊右移了labelWidth的距離,image的右邊相對於label的左邊右移了labelWidth的距離

獲取button內的image和label的寬度

1 CGFloat imageWidth = jumpBtn.imageView.bounds.size.width;
2 CGFloat labelWidth = jumpBtn.titleLabel.bounds.size.width;
3 jumpBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
4 jumpBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth);

同樣的,label的右邊相對於原button的右邊左移了imageWith的距離,label的左邊相對於image的右邊左移了imageWith的距離

負值是因為這是contentInset,是偏移量,不是距離

1 jumpBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
2 jumpBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM