私はテキストを持たないUIButtonを持っています。私は使用したい2つの画像を持っています(1つは通常の状態用、もう1つは選択状態用)。画像はボタンのサイズより小さくなります。UIButtonのUIImageリサイズを防止する
ボタンを描いたときにどちらの画像も拡大/縮小されないようにするにはどうすればよいですか? imageViewプロパティを設定すると、通常の状態ではスケールが正しく変更されますが、選択されている場合はスケールが正しく変更されません。
int topBottom = (button.frame.size.height - imageHeight)/2;
int leftRight = (button.frame.size.width - imageWidth)/2;
button.imageEdgeInsets = UIEdgeInsetsMake(topBottom,leftRight,topBottom,leftRight);
をそして、あなたはcontentMode /スケールファクタを設定する必要はありません。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:imageNormal forState:UIControlStateNormal];
[button setImage:imageSelected forState:UIControlStateSelected];
// this shows the correct scale in normal mode but not when button is tapped
button.imageView.contentScaleFactor = 1.0;
button.imageView.contentMode = UIViewContentModeCenter;
**注!!! **背景画像のみがフレームに合わせてデフォルトで引き伸ばされています。あなたがそれを中心にしたい場合は、 "画像"と背景画像を使用してください – Fattie