我想将我的UIToolbar设为纯灰色。这必须适用于iOS 4.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)];
}
没门。工具栏始终为黑色。
有什么线索吗?
谢谢。
最佳答案
您可以尝试使用纯灰色背景图像。您可以使用所需的颜色制作1x1像素的灰色png图像。然后使用以下代码将其设置为背景图片,它将在所有iOS版本(包括4.3及更低版本)上运行。
UIToolbar *toolBar = //...your 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];
}
关于iphone - iPhone-尝试制作纯色UIToolbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13042267/