1

私はかなり奇妙な問題を抱えています。1つのビューには、それぞれ画像とタイトルが付いた3つのボタンがあります。最初のボタンはOK、2番目は画像のみを表示、3番目は右の画像を表示しますが、タイトルはButton 2。以下のように:

------------
I  Image 1 I
I  Title 1 I
------------

------------
I  Image 2 I
I          I
------------

------------
I  Image 3 I
I  Title 2 I
------------

これを引き起こしている原因がわかりません。

次のようなカスタムinitメソッドを持つUIButtonサブクラスがあります。

- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)image andTitle:(NSString *)title
{

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];

        UIImageView *icon = [[UIImageView alloc] initWithImage:image];
        icon.backgroundColor = [UIColor clearColor];
        CGRect iconFrame = icon.frame;
        iconFrame.origin.x = frame.size.width / 2 - iconFrame.size.width / 2;
        iconFrame.origin.y = 5;
        iconFrame.size.height = frame.size.height / 2.5;
        icon.frame = iconFrame;

        [self addSubview:icon];

        UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont fontWithName:@"TisaMobiPro" size:14];
        [titleLabel setText:title];
        titleLabel.textColor = [UIColor colorWithRed:1.000 green:1.000 blue:1.000 alpha:0.5];
        titleLabel.backgroundColor = [UIColor clearColor];

        CGRect titleFrame = titleLabel.frame;
        titleFrame.origin.y = icon.frame.origin.y + icon.frame.size.height;
        titleFrame.size.height = frame.size.height / 2;
        titleLabel.frame = titleFrame;
        [self addSubview:titleLabel];
    }
    return self;
}

次のようなボタンを追加します。

int numberOfButtons = 3;

CGRect mentionRect = CGRectMake(0, 10,self.menuContainer.frame.size.width / numberOfButtons, self.menuContainer.frame.size.height - 10);
self.mentionButton = [[CustomBarButton alloc] initWithFrame:mentionRect withImage:[UIImage imageNamed:@"Images/Mentions.png"] andTitle:@"Mention"];
[self.menuContainer addSubview:self.mentionButton];


CGRect hashRect = CGRectMake(mentionRect.origin.x + mentionRect.size.width, 10,self.menuContainer.frame.size.width / numberOfButtons, self.menuContainer.frame.size.height - 10);
self.hashtagButton = [[CustomBarButton alloc] initWithFrame:hashRect withImage:[UIImage imageNamed:@"Images/HashTagIcon.png"] andTitle:@"Hashtag"];
[self.menuContainer addSubview:self.hashtagButton];


CGRect photoRect = CGRectMake(hashRect.origin.x + hashRect.size.width, 10, self.menuContainer.frame.size.width / numberOfButtons, self.menuContainer.frame.size.height - 10);
self.photoButton = [[CustomBarButton alloc] initWithFrame:photoRect withImage:[UIImage imageNamed:@"Images/CameraIcon.png"] andTitle:@"Photo"];
[self.menuContainer addSubview:self.photoButton];

誰かがこれを引き起こしているものを見つけることができますか、またはそれを解決するために私が何ができるかについて何か考えがありますか?

4

1 に答える 1

1

のフレーム設定を確認してくださいUILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];frameinitメソッドの入力パラメータとしてframeを設定しないでください。静的フレームを設定してから、要件に応じて変更し、そのビューの必要な場所に配置してください。基本的にあなたのorigin.x価値は間違っており、それがあなたが奇妙な出力を得る理由です。

于 2013-03-03T11:36:24.647 に答える