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

时间:2013-08-05 17:27:41

标签: 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 );
}

6 个答案:

答案 0 :(得分:8)

是的,因为您已经覆盖了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 );

}

根据需要更改帧内的值。

答案 1 :(得分:2)

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

#1。使用CGRect函数

UIColor子类中使用UIView实例绘制并填充指定的UIRectFill(_:)实例

UIKit提供了UIRectFill(_:)功能。 UIRectFill(_:)有以下声明:

func UIRectFill(_ rect: CGRect)
  

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

以下Playground代码显示了如何使用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。使用CGRect UIColor方法

UIView子类中使用CGContext实例绘制并填充指定的fill(_:)实例

CGContext有一个名为fill(_:)的方法。 fill(_:)有以下声明:

func fill(_ rect: CGRect)
  

使用当前图形状态下的填充颜色绘制所提供矩形中包含的区域。

以下Playground代码显示了如何使用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

答案 2 :(得分:1)

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

如果这是在View Controller内而不是View子类中,请在viewDidLoad方法中完全相同。

布赖恩

答案 3 :(得分:1)

您可以使用此代码。请根据您的意愿更改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

答案 4 :(得分:0)

您可以执行CGRect并剪切部分内容以填充。

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

巴拉斯

答案 5 :(得分:0)

覆盖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:方法。根据您的评论视图,我感觉这不是您正在做的事情。