2 garaster garaster 于 2012.10.25 17:11 提问

设置iphone中的UIToolbar的颜色

在ios4.3以上版本,想把UIToolbar的颜色改成灰色,

但是下面的代码没有实现:

- (void)drawRect:(CGRect)rect {

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);

        [[UIColor colorWithRed:64.0f/255.0f
                         green:64.0f/255.0f
                          blue:64.0f/255.0f
                         alpha:1.0f] set];

    CGRect myRect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    CGContextFillRect(ctx, myRect);

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

    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

为什么?

1个回答

g989_1314125
g989_1314125   2012.10.26 14:44
已采纳

可以通过使用灰色的背景图片实现。用下面代码加入图片:

   UIToolbar *toolBar = //此处添加toolbar

    UIImage *toolbarBkg = [[UIImage imageNamed: @"toolbarBkg.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0];

    if ([toolBar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)])
        [toolBar setBackgroundImage:toolbarBkg forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    else {
        UIImageView *background = [[UIImageView alloc] initWithImage:toolbarBkg];
        background.frame = toolBar.frame;
        [toolBar insertSubview:background atIndex:0];
    }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!