How i set the background color in UIView using CGContext?

前端 未结 2 2061
渐次进展
渐次进展 2021-02-20 01:10

I have developed the application in which i want to set the background color of UIView which is already set on UIViewController.The code is below,

@implementatio         


        
相关标签:
2条回答
  • 2021-02-20 01:27

    Swift 3

    UIGraphicsBeginImageContextWithOptions(size, true, UIScreen.main.scale)
    let context = UIGraphicsGetCurrentContext()
    context?.setFillColor(UIColor.green.cgColor)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
    
    0 讨论(0)
  • 2021-02-20 01:42

    How about this code?

    - (void)drawRect:(CGRect)rect {
    
        [super drawRect:rect];
        CGContextRef context = UIGraphicsGetCurrentContext(); 
    
        CGRect drawRect = CGRectMake(rect.origin.x, rect.origin.y,rect.size.width, rect.size.height);
    
        CGContextSetRGBFillColor(context, 100.0f/255.0f, 100.0f/255.0f, 100.0f/255.0f, 1.0f);
        CGContextFillRect(context, drawRect);
    }
    
    0 讨论(0)
提交回复
热议问题