0

I wanna to make buttons for my app flat & transaparent, like the ones in the kindle app (see the shop button).

I try to set a custom background by code with not luck (the button is draw as a rectangle, with he border color fill all the background):

- (UIImage *) screenshot {
    // Create a graphics context with the target size
    // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
    // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
    CGSize imageSize = self.bounds.size;

    if (NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    else
        UIGraphicsBeginImageContext(imageSize);

    CGContextRef context = UIGraphicsGetCurrentContext();

    [self.layer renderInContext:context];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;   
}

+(UIImage *) buttonFlat {
    UIView *bt = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 1.0f, 1.0f)];

    bt.backgroundColor = [UIColor blackColor];
    bt.layer.borderColor = [UIColor lightGrayColor].CGColor;
    bt.layer.borderWidth = 1;
    bt.layer.cornerRadius = 5.0;
    bt.alpha = 0.7;

    return [bt screenshot];
}

I know how do this with a normal UIButton, but prefer to make this so I can retain the standard icons of the UIBarButtonItem if possible...

mamcx
  • 15,916
  • 26
  • 101
  • 189

1 Answers1

1

A UIBarButton can hold any custom view:

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:someView];
Kevin
  • 3,111
  • 1
  • 19
  • 26
  • This will requiere to ditch interface builder and do all of them by code? – mamcx Oct 10 '11 at 21:51
  • 1
    Not necessarily. What I've done in the past is added a UIButton to Interface Builder but not added it to any view (just floats out there alone). Create an IBOutlet for that button and then you can still use it in the initWithCustomView – Kevin Oct 11 '11 at 11:44