Ios 仅为UIView的一部分设置背景色
我希望UIView的底部(不是一半)与顶部的颜色不同 我想知道我是否应该创建一个CGRect,然后给它上色?这条路对吗Ios 仅为UIView的一部分设置背景色,ios,uiview,cgrect,uibackgroundcolor,Ios,Uiview,Cgrect,Uibackgroundcolor,我希望UIView的底部(不是一半)与顶部的颜色不同 我想知道我是否应该创建一个CGRect,然后给它上色?这条路对吗 - (void)drawRect:(CGRect)rect { CGRect aRect = CGRectMake(x, y, width, height); // Fill the rectangle with grey [[UIColor greyColor] setFill]; UIRectFill( rect ); } 您可以执行
- (void)drawRect:(CGRect)rect {
CGRect aRect = CGRectMake(x, y, width, height);
// Fill the rectangle with grey
[[UIColor greyColor] setFill];
UIRectFill( rect );
}
您可以执行CGRect并剪裁要填充的部分的一部分 但是为什么不尝试相邻放置两个不同的UIView呢
Bharath是的,因为您已经覆盖了drawRect方法,这就可以了
- (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 );
}
根据需要更改框架内的值。覆盖UIView子类的drawRect方法。以下代码将使视图的上半部分变为黑色,下半部分变为红色
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Top View
CGRect topRect = {CGRectGetMinX(self.bounds), CGRectGetMinY(self.bounds), CGRectGetMaxX(self.bounds), CGRectGetMaxY(self.bounds)};
[[UIColor blackColor] setFill];
UIRectFill(topRect);
// Bottom View
CGRect bottomRect = {CGRectGetMinX(self.bounds), CGRectGetMidY(self.bounds), CGRectGetMaxX(self.bounds), CGRectGetMidY(self.bounds)};
[[UIColor redColor] setFill];
UIRectFill(bottomRect);
}
注意:此外,当您为UIView子类化时,您只能重写drawRect:方法。根据您的评论视图,我感觉这不是您正在做的。您也可以将CALayer作为子层添加到视图中。 包括CoreGraphics和QuartzCore框架,并在drawRect方法中创建具有所需形状因子的CGRect。 使用rect实例化CALayer,并使用[self.layer addSublayer:theLayer]将其添加到视图的层中。 在添加之前,请使用CALayer的-setBackgroundColor:方法 如果这是在视图控制器而不是视图子类中,请在viewDidLoad方法中执行完全相同的操作
Bryan您可以使用此代码。请根据您的要求更改CGRect
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect topView = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 2);
CGRect bottomView = CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 2, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 2);
UIColor * grayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];
CGContextSetFillColorWithColor(context, grayColor.CGColor);
CGContextFillRect(context, bottomView);
CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextFillRect(context, topView);
}
下面的链接可能对您有更多帮助。
使用Swift 5.1和iOS 13,您可以选择以下两种方法之一来解决问题
#1.使用
UIRectFill(:)
函数在UIView
子类中使用UIColor
实例绘制并填充指定的CGRect
实例
UIKit
提供了UIRectFill(:)
功能UIRectFill(:)
具有以下声明:
func UIRectFill(_ rect: CGRect)
func fill(_ rect: CGRect)
用当前颜色填充指定的矩形
以下游乐场代码显示了如何使用UIRectFill(:)
:
#2.使用
CGContext
的fill(:)
方法,在UIView
子类中使用UIColor
实例绘制并填充指定的CGRect
实例
CGContext
有一个名为fill(:)
的方法<代码>填充(:)具有以下声明:
func UIRectFill(_ rect: CGRect)
func fill(_ rect: CGRect)
使用当前图形状态下的填充颜色绘制所提供矩形中包含的区域
以下游乐场代码显示了如何使用填充(:)
:
当我实现您的代码时,我得到一个错误,即“在对象类型[myUIView]上找不到属性'frame'”。您是否正在对UIView进行子类化?如果是这样的话,那么你应该可以访问frame属性。另外,我相信发布的代码是不正确的。您想用
self.bounds
而不是self.frame
来定义topRect和bottomRect,因为frame
是用superview的坐标空间来定义的,而bounds
是用视图的坐标空间来定义的,这是自定义绘图所需要的。@ZevEisenberg这是正确的,边界在某些情况下很有用。但在这种情况下,当UIView子类被实例化时,框架被设置为其superview。当视图的原点不在其superview的坐标空间中的{0,0}时,在此处使用frame
是不正确的,或者,当视图上存在可能导致帧
和边界
不同的变换时。当我将代码放入.m文件中时,不会发生任何变化。drawRect是自动调用的,对吗?你把它放在哪个.m文件中?是的,它是自动调用的,但drawRect是UIView类的一个方法。你真的覆盖了UIView吗?就像我说的,drawRect是UIView类的一个方法。您必须创建一个覆盖UIView的类,并将其添加为viewcontroller主视图的子视图。您需要创建一个UIView子类以覆盖drawRect方法。如果有帮助,请批准答案。您的理论很清楚,但请给出一些代码,这对我完全有帮助。哪一个更好#1还是#2?