0

私が欲しいのは、UIButton isSelected のときに UIButton を UIView メニューに準拠させることです。UINavigation の黒い四角は、UIButton が選択された後の UIImage です。私の質問は、選択後に UIImage (黒い四角) を UIButton よりも高くする方法です。

スクリーンショット

これはUIButton Class .mの私のコードです:

- (id)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];

        if (self) {
            //...Our red stretchy
            UIImage *redImage = [UIImage imageNamed:@"button_red.png"];
            UIImage *redStretchy = [redImage stretchableImageWithLeftCapWidth:redImage.size.width / 2 topCapHeight:redImage.size.height / 2];

            //...Initialization code
            [self setFrame:frame];
            [self setTitle:TITLE forState:UIControlStateNormal];
            [self setBackgroundImage:redStretchy forState:UIControlStateNormal];
            [[self titleLabel] setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]];
            [[self titleLabel] setShadowOffset:CGSizeMake(FONT_SHADOW_OFFSET, FONT_SHADOW_OFFSET)];

            //...add target
            [self addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
        }

        return self;
    }

これはターゲットメソッドです:

    //...add target
- (void)actionButton:(id)sender {

    if ([sender isSelected]) {

    } else {

        CGSize buttonSize = CGSizeMake(self.frame.size.width, self.frame.size.height);

        [sender setImage:[self imageButton:buttonSize] forState:UIControlStateSelected];

    }

    if (delegate != nil && [delegate conformsToProtocol:@protocol(ButtonProtocol)]) {
        if ([delegate respondsToSelector:@selector(actionButton:)]) {
            [delegate performSelector:@selector(actionButton:) withObject:sender];
        }
    }

}

そして、このメソッドはUIIMageを作成します:

    //...create image button
- (UIImage *)imageButton:(CGSize)size {

    UIGraphicsBeginImageContextWithOptions(size, NO, 0);

    CGContextRef currentContex = UIGraphicsGetCurrentContext();

    CGMutablePathRef path = CGPathCreateMutable();

    CGRect firstRect = CGRectMake(0.0f, 0.0f, 60.f, 30.0f);

    CGPathAddRect(path, NULL, firstRect);

    CGContextAddPath(currentContex, path);

    UIColor *fillColor = [UIColor blackColor];

    CGContextSetFillColorWithColor(currentContex, fillColor.CGColor);

    CGContextDrawPath(currentContex, kCGPathFill);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}
4

0 に答える 0