53

カスタムUIButton画像がボタンでサイズ変更されないのはなぜですか?

ビューモードをScaletoFill in Interface Builderに設定しましが、UIImageViewイメージとは異なり、その設定を尊重しません。

私は間違った場所を見ていますか、それとも不可能ですか?

4

11 に答える 11

94

これはあなたが求めているものではないかもしれませんが、背景画像拡大縮小されます。

于 2009-06-30T11:06:14.477 に答える
69

これを試して:

[button setImage:image forState:UIControlStateNormal];
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;

Swiftの場合:

button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
于 2014-10-15T07:17:45.597 に答える
60

ボタンのコントロールアライメント設定を忘れずに設定してください

ここで、ボタンのコントロールアライメント設定を設定します

于 2016-12-13T16:33:20.403 に答える
13

これは古いことは知っていますが、正解は、コンテンツモードをUIButton内にある「非表示」のUIImageViewに設定する必要があるということだと思います。

button.imageView.contentMode = UIViewContentModeScaleAspectFill;

これにより、次のように設定された画像の画像ビューのコンテンツモードが変更されます。

[button setImage:yourAwesomeImage forState:UIControlStateNormal]; 
//Or any other state
于 2013-06-17T04:11:36.633 に答える
11

(デザインからまたはコードから):

デザインから

  1. xibまたはストーリーボードを開きます。
  2. 選択ボタン
  3. 内部属性インスペクター(右側)> [コントロール]セクションで、[水平]と[垂直]の両方の最後の4番目のオプションを選択します。

[ポイント#3の場合:水平および垂直の配置をUIControlContentHorizo​​ntalAlignmentFillおよびUIControlContentVericalAlignmentFillに変更]

コードから

button.contentHorizo​​ntalAlignment = UIControlContentHorizo​​ntalAlignmentFill; button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;

于 2015-09-11T08:26:49.593 に答える
9

InterfaceBuilder

UIButtonsの画像をInterfaceBuilderのUIViewsと同じように拡大縮小するには、次の手順に従います。

UIButtonを選択し、IdentityInspectorからユーザー定義のランタイム属性に以下を追加します。

imageView.contentMode番号1

ここで、numberはcontentModeの列挙数です。例:

0 = Scale to fill 
1 = Aspect Fit 
2 = Aspect Fill
etc..

次に、UIButtonの属性エディタを開き、[コントロール]で、水平方向と垂直方向の両方で[配置]を[塗りつぶし](右側の最後のボタン)に設定します。

SWIFT 3

次のコードでこれを行うこともできることに注意してください。

button.imageView?.contentMode = .scaleAspectFit
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
于 2017-05-20T01:04:27.940 に答える
8

属性インスペクターの内部では、最初にタイプ" Custom"とスタイル" Default"を設定し、次に位置合わせを水平と垂直の両方に設定します" fill"。

ここに画像の説明を入力してください

于 2021-12-14T13:28:34.820 に答える
2

この投稿を更新すると、Swift5の完全な回答になります。

//Set button's image
let image = UIImage(systemName: "heart.fill") //Your image here
button.setImage(image, for: .normal)

//Optional: Remove background image if you added one on accident
button.setBackgroundImage(nil, for: .normal)

//Optional: Set button's title (displayed next to button's image)
button.setTitle(nil, for: .normal)

//The button's "content" includes the title label and image
//So we set the button's content to fill the button
//If title label is nil, the content will only show the image
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill

//Set button's image to desired content mode (aspectFit avoids distortion)
//This restricts the image from resizing to fill the entire content area
button.imageView?.contentMode = .scaleAspectFit
于 2020-04-18T17:53:05.867 に答える
0

InterfaceBuilderのClipsubviewsプロパティをtrueに設定してみてください。

お役に立てば幸いです。

于 2009-06-30T11:33:58.460 に答える
-1

Swift 2.1でこれまでに見つけた最善の解決策は、次のとおりです。

self.imageButton.imageView?.clipsToBounds = true
self.imageButton.imageView?.contentMode = UIViewContentMode.ScaleAspectFit

これにより、画像が拡大縮小され、imageViewにアスペクトが収まります。

于 2016-02-05T08:11:55.533 に答える
-6

実際のUIimageviewを取得し、その上にUIbuttonを配置し、レイアウトメニューを使用して戻るように設定するだけです。次に、画像を適切に拡大縮小できます。

于 2010-03-05T11:09:40.767 に答える