仅为UIView的一部分设置背景颜色 [英] Set background color for only part of UIView
本文介绍了仅为UIView的一部分设置背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的UIView的底部(不是一半)与顶部的颜色不同。
I want the bottom (not quite half) of my UIView to be a different color than the top.
我想知道我是否应该创建一个CGRect和那颜色呢?这是沿着正确的轨道吗?
I'm wondering if I should create a CGRect and then color that? Is this along the right track?
- (void)drawRect:(CGRect)rect {
CGRect aRect = CGRectMake(x, y, width, height);
// Fill the rectangle with grey
[[UIColor greyColor] setFill];
UIRectFill( rect );
}
推荐答案
是的,因为你已经重写drawRect方法,这样做。
Yes, as you are already overriding drawRect method, this will do.
- (void)drawRect:(CGRect)rect {
CGRect topRect = CGRectMake(0, 0, rect.size.width, rect.size.height/2.0);
// Fill the rectangle with grey
[[UIColor greyColor] setFill];
UIRectFill( topRect );
CGRect bottomRect = CGRectMake(0, rect.size.height/2.0, rect.size.width, rect.size.height/2.0);
[[UIColor redColor] setFill];
UIRectFill( bottomRect );
}
根据需要更改帧内的值。
Change the values inside the frames as you wish.
这篇关于仅为UIView的一部分设置背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文