1

カスタム UIButton クラスを作成し、 init Method を上書きしました:

- (id)initWithFrame:(CGRect)frame title:(NSString *)titulo
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        setaDown = [UIImage imageNamed:@"setaDown"];
        separator = [UIImage imageNamed:@"separatorLine"];

        self = [UIButton buttonWithType:UIButtonTypeCustom];
        [self setFrame:frame];
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        self.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        seta = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width-setaDown.size.width*2, frame.size.height/2, setaDown.size.width/1.2, setaDown.size.height/1.2)];
        [seta setImage:setaDown];
        [self addSubview:seta];
        separatorLine = [[UIImageView alloc]initWithFrame:CGRectMake(5, frame.size.height-2, frame.size.width-10, separator.size.height)];
        [separatorLine setImage:separator];
        [self addSubview:separatorLine];
        [self setTitle:titulo forState:UIControlStateNormal];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

問題は、画像がボタンに表示されずexc_bad_access、行に , があることです:

seta = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width-setaDown.size.width*2, frame.size.height/2, setaDown.size.width/1.2, setaDown.size.height/1.2)];

このエラーが発生する理由とその解決方法を正確に知りません。

4

3 に答える 3

0

UIButtonはすでに がありimageViewます。簡単に使用できます

self.imageView.image = setaDown

必要に応じて のその他の側面を構成imageViewします (たとえば、separatorフィットするように位置とサイズを調整します)。

于 2013-04-24T23:24:00.427 に答える
0

私の知る限り、ボタンに 2 つの画像を追加することはできません。画像エディターを入手し、それらを合成してから、次のようにします。

[self setBackgroundImage:[UIImage imageNamed:@"mynewimagename"] forState:UIControlStateNormal]

はるかに簡単です。

于 2013-04-24T23:53:50.600 に答える