28

がありUIButton、タイトルと画像を設定しようとしています。

a のタイトルをUIButton左側に配置し、画像を右側に配置したいと考えています。時計アプリのタイマーのボタンのルックアンドフィールを取得しようとしています(「タイマーが終了したとき」と表示されているもの)。

contentHorizontalAlignmentcontentEdgeInsetstitleEdgeInsetsをいじっimageEdgeInsetsて目標を達成しましたが、役に立ちませんでした。ドキュメントも同じように非常にまばらです。

どうすれば同じことを達成できますか?

また、関連する質問、Timer in Clocks アプリには 2 つのテキスト セットがあり、1 つは画像の左に、もう 1 つは右に配置されていますか? どのようにそれを行うことができUIButtonますか?(現時点ではその機能は必要ありません)。

4

9 に答える 9

49

そのために使用するコードは次のとおりです。

//Right-align the button image
CGSize size = [[myButton titleForState:UIControlStateNormal] sizeWithFont:myButton.titleLabel.font];
[myButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -size.width)];
[myButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, myButton.imageView.image.size.width + 5)];
于 2011-11-07T10:15:37.000 に答える
35

imageEdgeInsetsのプロパティを変更してUIButtonから使用しますsetImage:forState:

于 2010-10-25T15:56:12.930 に答える
14

次のコードが機能します。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = buttonRect;
[button setTitle:@"A title" forState:UIControlStateNormal];
[button setImage:buttonImage forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0.0, WIDTH(button.titleLabel) + 10.0, 0.0, 0.0);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
于 2012-07-10T15:38:05.880 に答える
9

UIButton は UIView を継承しているため、他のビューと同じようにサブビューを追加できることに注意してください。あなたの状況では、ボタンを作成してから、左側に UILabel を追加し、右側に UIImage を追加します。

// Create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

// Now load the image and create the image view
UIImage *image = [UIImage imageNamed:@"yourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(/*frame*/)];
[imageView setImage:image];

// Create the label and set its text
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(/*frame*/)];
[label setText:@"Your title"];

// Put it all together
[button addSubview:label];
[button addSubview:imageView];
于 2009-07-12T14:40:32.883 に答える
4

iOS 9 で使用するために、このスレッドで多くの回答を試しましたが、どれも私には適していませんでした。私は次のように自分自身の答えを見つけました:

class MyButton: UIButton {

  override func layoutSubviews() {
    super.layoutSubviews()

    self.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left

    if self.imageView?.image != nil {
      // Move icon to right side
      self.imageEdgeInsets = UIEdgeInsets(
        top: 0,
        left: self.bounds.size.width - self.imageView!.image!.size.width,
        bottom: 0,
        right: 0)

      // Move title to left side
      self.titleEdgeInsets = UIEdgeInsetsMake(0, -self.imageView!.frame.size.width + 8, 0, 0)

    }
  }
}

スイフト 3:

class MyButton: UIButton {

    override func layoutSubviews() {
        super.layoutSubviews()

        self.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left

        if self.imageView?.image != nil {
            // Move icon to right side
            self.imageEdgeInsets = UIEdgeInsets(
                top: 0,
                left: self.bounds.size.width - self.imageView!.image!.size.width,
                bottom: 0,
                right: 0)

            // Move title to left side
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -self.imageView!.frame.size.width + 8, 0, 0)

        }
    }
}

私のようなケースの誰かに役立つことを願っています!

于 2016-09-30T08:31:37.413 に答える
4

titleRectForContentRect:のメソッドとimageRectForContentRect:メソッドをオーバーライドしてUIButton、テキストと画像を好きな場所に配置することもできます。

于 2013-08-09T18:25:30.853 に答える
1

UIButtonこれは、画像を左に、テキストを中央に配置する Swiftのサブクラスです。希望する値に設定imageLeftInsetします。

class LeftImageAlignedButton : UIButton {

var imageLeftInset : CGFloat = 10.0

override func layoutSubviews() {
    super.layoutSubviews()
    self.contentHorizontalAlignment = .Left
    if let font = titleLabel?.font {
        let textWidth = ((titleForState(state) ?? "") as NSString).sizeWithAttributes([NSFontAttributeName:font]).width
        let imageWidth = imageForState(state)?.size.width ?? 0.0
        imageEdgeInsets = UIEdgeInsets(top: 0, left: imageLeftInset, bottom: 0, right: 0)
        titleEdgeInsets = UIEdgeInsets(top: 0, left: bounds.width/2 - textWidth/2.0 - imageWidth, bottom: 0, right: 0)
    }
}
}
于 2015-05-13T15:47:12.090 に答える
-2

UIButton サブクラスを作成し、そのlayoutSubviewsメソッドをオーバーライドして、テキストと画像をプログラムで整列させます。または、https://github.com/stevestreza/BlockKit/blob/master/Source/UIView-BKAdditions.hのようなものを使用して、ブロックを使用して layoutSubviews を実装できるようにします。

于 2012-03-01T14:03:52.673 に答える