如何以编程方式将平坦的背景UIColor渲染为UIToolbar的背景?
|
因此,我遵循了一个教程,该教程允许我继承UIToolbar并绘制图像作为UIToolbar的自定义背景。
代码是这样的:
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
UIImage *backgroundImage = [UIImage imageNamed:@\"toolbar_background.png\"];
[backgroundImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
这完美无瑕。考虑到我只希望工具栏背景为纯色。基本上像[UIColor blackColor]这样的东西,在drawRect方法中是否有更简单的方法?
当[UIColor blackColor]可用时,必须制作高度为320 x 44 px的纯黑色背景图像,并将其与上述代码一起使用似乎会产生极大的负担?我只是不确定如何在这里实现它。
我考虑过做这样的事情:
- (void)drawRect:(CGRect)rect
{
UIView *test = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
test.backgroundColor = [UIColor blackColor];
[self addSubview:test];
}
但这是行不通的,因为UIView会覆盖我稍后添加的所有UIToolbar项,即Toolbar是黑色的,但是黑色覆盖了所有工具栏项,因此它们不可见。
有解决方法吗?
谢谢。
没有找到相关结果
已邀请:
2 个回复
镰茧钩
方法,但是请使用
方法代替绘制图像,如下所示:
坝镰补翔奋