【问题标题】:How make a transparent & flat UIBarButtonItem?如何制作透明且扁平的 UIBarButtonItem?
【发布时间】: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


【解决方案1】:

一个 UIBarButton 可以容纳任何自定义视图:

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

【讨论】:

  • 这将需要放弃界面构建器并通过代码完成所有这些操作?
  • 不一定。我过去所做的是将 UIButton 添加到 Interface Builder 但没有将其添加到任何视图(只是单独漂浮在那里)。为该按钮创建一个 IBOutlet,然后您仍然可以在 initWithCustomView 中使用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-14
  • 2011-04-08
相关资源
最近更新 更多