如何制作一个透明的&扁平的UIBarButtonItem?

时间:2011-10-10 18:35:08

标签: objective-c xcode4 ios4 uibarbuttonitem ios5

我想为我的app flat& transapearent,就像kindle应用程序中那些(see the shop button)。

我尝试通过代码设置自定义背景而没有运气(按钮绘制为矩形,边框颜色填充所有背景):

- (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];
}

我知道如何使用普通的UIButton,但更喜欢这样做,所以我可以保留UIBarButtonItem的标准图标,如果可能的话......

1 个答案:

答案 0 :(得分:1)

UIBarButton可以保存任何自定义视图:

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:someView];