iOSノート:Buttonの画像と文字の左右の位置を交換する

1178 ワード

titleEdgeInsetsプロパティとimageEdgeInsetsプロパティは、このbuttonを描くときにimageとlabelの位置を調整するために使用されるプロパティだけで、button自体のサイズには影響しません.これらはimageとbuttonの元の位置に対するオフセット量にすぎない.
イメージが右にある場合、labelが左にある場合、イメージの左はbuttonの左に対してlabelWidthの距離を右に移動し、イメージの右は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-15, 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);