【发布时间】:2011-10-10 18:35:07
【问题描述】:
我想让我的应用程序的按钮扁平透明,就像 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 的标准图标...
【问题讨论】:
标签: objective-c xcode4 ios4 uibarbuttonitem ios5