不会出现在代码中创建的UITabBarItem图像 [英] UITabBarItem image created in code doesn’t appear

查看:79
本文介绍了不会出现在代码中创建的UITabBarItem图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中创建了以下View,想法是将它用作 UITableViewCell UITabBarItem 中的图像:

I create the following View in code, the idea is to use it as an image in UITableViewCell and UITabBarItem:

- (void)drawRect:(CGRect)rect
{
    // Fill the background with white
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor * whiteColor = [UIColor colorWithRed:1.0
                                           green:1.0
                                            blue:1.0
                                           alpha:1.0];

    CGContextSetFillColorWithColor(context, whiteColor.CGColor);
    CGContextFillRect(context, self.bounds);

    //// Color Declarations
    UIColor* fillColor = [UIColor colorWithRed: 1.0
                                         green: 1.0
                                          blue: 1.0
                                         alpha: 1.0];

    UIColor* strokeColor = [UIColor colorWithRed: 0.0
                                           green: 0.0
                                            blue: 0.0
                                           alpha: 1.0];

    //// Big Oval Drawing
    CGRect bigOvalRect = CGRectMake(rect.origin.x+2,
                                    rect.origin.y+5,
                                    42.0,
                                    42.0);

    UIBezierPath* bigOvalPath = [UIBezierPath bezierPath];

    [bigOvalPath addArcWithCenter: CGPointMake(CGRectGetMidX(bigOvalRect), CGRectGetMidY(bigOvalRect))
                         radius: CGRectGetWidth(bigOvalRect) / 2
                     startAngle: 40 * M_PI/180
                       endAngle: 320 * M_PI/180
                      clockwise: YES];

    [fillColor setFill];
    [bigOvalPath fill];
    [strokeColor setStroke];
    bigOvalPath.lineWidth = 1;
    [bigOvalPath stroke];

 }

从这个UIView创建一个UIImage我正在使用这个代码:

To create an UIImage from this UIView I am using this code:

#import <QuartzCore/QuartzCore.h>

+ (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;
}

当我在UITableViewCells中使用它时,一切都按预期工作但是当我设置它时作为我的UITabBarItem的图像我只看到一个蓝色矩形(iOS7):

Everything works as expected when I use it in my UITableViewCells but when I set it as the image of my UITabBarItem I only see a blue rectangle (iOS7):

MyIcon *icon = [[MyIcon alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 50.0)];
UIImage *tabImage =[self imageWithView:icon];

self.navigationController.tabBarItem.image = tabImage;

我想这与图像的 alpha 有关但是如何我能解决吗?

I guess it is something related with the alpha of the image but how can I fix it?

推荐答案

这不是与视图大小相关的问题,而是与其alpha相关的问题。

It wasn't a problem related to the size of the view but to its alpha.

我通过将backgroundColor(was whiteColor)和fillColor设置为:

I fixed it by setting the backgroundColor (was whiteColor) and the fillColor to:

[UIColor clearColor]

并在 initWithFrame:方法:

[self setOpaque = NO];

这篇关于不会出现在代码中创建的UITabBarItem图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆