1

デフォルトのUIButton画像と、ハイライト/選択した画像用の別の画像があります。ボタンを押すと、ボタンの画像が強調表示され、タッチアップが内側にある場合は選択された画像に変わります。通常のもの..IB内のすべてのセットアップ。

ただし、非常にトリッキーな場所(位置合わせされていない、ハードコーディングされていない)で、ボタンの上にラベルを設定しようとしています。

IBのボタンにラベルを付けてみました。問題:ボタンの制御状態が変化すると、ラベルのテキストの色を変更する必要があります。

そこで、UIButtonサブクラスを作成し、UILabelOutletを追加し、次のメソッドをオーバーライドしました。

- (void)touchesBegan/Moved/Cancelled/Ended:...;
- (void)setSelected:...

やりたいことができました…でも!ボタンをすばやくクリックしても、変更は反映されません。時々それは正しく動作しません...私は非同期呼び出しさえ使用しました...使用しません。

UIButtonそれで、私は'sに向かいましたtitleLabel。運が悪かったので使ってみました。

だから、私は試しましUIButton setTitle: forState:た、役に立たない...助けて?


追加の詳細:

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self){
        [self.titleLabel setFrame:CGRectMake(0、0、100、100)];
        [self.titleLabel setText:@ "THE TITLE LABEL"];
        [self.titleLabel setHidden:NO];

        [self.imageView setAlpha:0.2f];
        NSLog(@ "%@"、self.subviews);


        [self setTitle:@ "DEFAULT !!" forState:UIControlStateNormal];
    }
    自己を返す;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self PerformSelector:@selector(check)withObject:nil afterDelay:0.1];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    [self PerformSelector:@selector(check)withObject:nil afterDelay:0.1];

}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    [self PerformSelector:@selector(check)withObject:nil afterDelay:0.1];

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self PerformSelector:@selector(check)withObject:nil afterDelay:0.1];

}

-(void)setSelected:(BOOL)selected {
    [スーパーsetSelected:selected];
    [self PerformSelector:@selector(check)withObject:nil afterDelay:0.1];
}

-(void)チェック{
    if(self.isSelected || self.state == UIControlStateHighlighted || self.state == UIControlStateSelected){
        [_label setHighlighted:YES];
    } そうしないと {
        [_label setHighlighted:NO];
    }
}

出力:

(
    "<UIImageView: 0x8b24930; frame = (0 0; 243 39); clipsToBounds = YES; alpha = 0.2; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8b248e0>>",
    "<UIButtonLabel: 0x8b247a0; frame = (0 0; 100 100); text = 'THE TITLE LABEL'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8b25000>>"
)
4

2 に答える 2

0

これらすべての月の後に私が見つけた最良の答えは、オーバーライドすることです:

- (CGRect)titleRectForContentRect:(CGRect)contentRect;

ラベルを好きなように配置するには。これにより、次を利用できるようになります。

[self setTitleColor:[UIColor offWhiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor burntCarbonColor] forState:UIControlStateSelected];
[self setTitleColor:[UIColor burntCarbonColor] forState:UIControlStateHighlighted];

もっと好奇心旺盛な人のために:

- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    CGSize margin = CGSizeMake(15.f, 5.f);
    CGRect clay = contentRect;
    clay = CGRectInset(clay, margin.width, 0.f);
    clay.origin.x += margin.width;
    clay.origin.y += margin.height;

    return clay;
}
于 2012-10-12T11:36:44.620 に答える
0

UIButtonの背景画像を設定する必要があります...ボタンの画像プロパティを設定している可能性があります...そのため、ラベルが画像と重なっています....

[optionButtonOutlet  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

 [optionButtonOutlet setBackgroundImage:[UIImage imageNamed:@"predict_match_btn_blue_pressed@2x.png" ]forState:UIControlStateNormal];
于 2012-10-12T05:43:40.957 に答える