2

非常に単純な質問であることを願っていますが、次のコード (arc と *.nibs を使用しないプロジェクト) の後に UIButton のフレームをどのように合わせることができますか?

    _fotosButton = [UIButton buttonWithType: UIButtonTypeCustom];
    [_fotosButton setImage: [UIImage imageNamed: @"kino_gallery_normal.png"] forState: UIControlStateNormal];
    [_fotosButton setImage: [UIImage imageNamed: @"kino_gallery_highlight.png"] forState: UIControlStateHighlighted];
    [_fotosButton setTitle: NSLocalizedString(@"FOTOS", nil) forState: UIControlStateNormal];
    [_fotosButton setTitleColor: RGBCOLOR(11.0f, 176.0f, 225.0f) forState: UIControlStateNormal];
    [_fotosButton setTitleColor: [UIColor grayColor] forState: UIControlStateDisabled];
    [_fotosButton.titleLabel setFont:TTSTYLEVAR(kinoFont10)];
    [_fotosButton sizeToFit];

    [self updateButtonInsets:_fotosButton];
    [self addSubview: _fotosButton];

- (void) updateButtonInsets:(UIButton*) button {

   CGSize imageSize = button.imageView.frame.size;
   CGSize titleSize = button.titleLabel.frame.size;

   button.titleEdgeInsets = UIEdgeInsetsMake(0.0f, -imageSize.width, -imageSize.height, 0.0);

   titleSize = button.titleLabel.frame.size;
   button.imageEdgeInsets = UIEdgeInsetsMake(-titleSize.height, 0.0f, 0.0f, -titleSize.width);

}

これは私のコードの結果です: ここに画像の説明を入力

では、不要なパディングなしでフレーム (緑色の領域) をコンテンツ (タイトル + 画像) に合わせるにはどうすればよいでしょうか。アプリはローカリゼーションを使用するため、ボタンのフレームの値をハードコーディングできません。ありがとうございました!

4

1 に答える 1

0

良い解決策ではないと思いますが、投稿させていただきます。どんなコメントでも大歓迎です))

titleSize = button.titleLabel.frame.size;
button.imageEdgeInsets = UIEdgeInsetsMake(-titleSize.height, 0.0f, 0.0f, -titleSize.width);

[button setSize:CGSizeMake(titleSize.width, titleSize.height + imageSize.height)];

ボタンのサイズを手動で設定したところ、現在はこのようになっています 切り取った

ありがとうございました

于 2013-11-28T10:28:09.653 に答える