只为UIView的一部分设置背景颜色

我想我的UIView底部(不是一半)是一个不同的颜色比顶部。

我想知道如果我应该创build一个CGRect,然后颜色? 这是沿着正确的轨道?

- (void)drawRect:(CGRect)rect { CGRect aRect = CGRectMake(x, y, width, height); // Fill the rectangle with grey [[UIColor greyColor] setFill]; UIRectFill( rect ); } 

是的,因为你已经重写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 ); } 

根据需要更改帧内的值。

你可以使用这个代码。 请根据您的意愿更改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); } 

以下链接可能会帮助你更多。 http://www.raywenderlich.com/32925/core-graphics-tutorial-shadows-and-gloss

你可以做一个CGRect并剪辑一部分的部分来填充。

但为什么不尝试两个不同的UIViews放在一起呢?

巴拉斯

您也可以将CALayer作为子图层添加到您的视图中。 包括CoreGraphics和QuartzCore框架,并在drawRect方法中创build一个具有所需形状因子的CGRect。 使用rect实例化CALayer,并使用[self.layer addSublayer:theLayer]将其添加到视图的图层中。 在添加之前使用CALayer的-setBackgroundColor:方法。

如果这是在一个视图控制器,而不是一个视图子类,在viewDidLoad方法完全相同。

布赖恩

重写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:方法。 根据你的评论视图,我有一种感觉,这不是你在做什么。

使用Swift 4和iOS 11,根据您的需要,您可以select以下两种方法之一来解决您的问题。


#1。 使用UIRectFill(_:)函数在UIView子类内使用UIColor实例绘制并填充指定的CGRect实例

UIKit提供了一个UIRectFill(_:)函数。 UIRectFill(_:)具有以下声明:

 func UIRectFill(_ rect: CGRect) 

用当前颜色填充指定的矩形。

以下游乐场代码显示如何使用UIRectFill(_:)

 import UIKit import PlaygroundSupport class CustomView: UIView { override init(frame: CGRect) { super.init(frame: frame) backgroundColor = UIColor.green } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func draw(_ rect: CGRect) { super.draw(rect) let bottomRect = CGRect( origin: CGPoint(x: rect.origin.x, y: rect.height / 2), size: CGSize(width: rect.size.width, height: rect.size.height / 2) ) UIColor.red.set() UIRectFill(bottomRect) } } let view = CustomView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) PlaygroundPage.current.liveView = view 

#2。 使用CGContextfill(_:)方法在UIView子类内绘制并填充指定的CGRect实例和UIColor实例

CGContext有一个名为fill(_:)fill(_:)具有以下声明:

 func fill(_ rect: CGRect) 

使用当前graphics状态的填充颜色绘制提供的矩形内包含的区域。

以下游乐场代码显示如何使用fill(_:)

 import UIKit import PlaygroundSupport class CustomView: UIView { override init(frame: CGRect) { super.init(frame: frame) backgroundColor = UIColor.green } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func draw(_ rect: CGRect) { super.draw(rect) let bottomRect = CGRect( origin: CGPoint(x: rect.origin.x, y: rect.height / 2), size: CGSize(width: rect.size.width, height: rect.size.height / 2) ) UIColor.red.set() guard let context = UIGraphicsGetCurrentContext() else { return } context.fill(bottomRect) } } let view = CustomView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) PlaygroundPage.current.liveView = view