Drawing a rectangle in UIView

Your code does not require the CGContextSetRGBFillColor call and is missing the CGContextStrokeRect call. With Swift 5, your final draw(_:) implementation should look like this:

class CustomView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        guard let ctx = UIGraphicsGetCurrentContext() else { return }    
        ctx.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 0.5)
        let rectangle = CGRect(x: 0, y: 100, width: 320, height: 100)
        ctx.stroke(rectangle)
    }

}

As an alternative, if your really want to call CGContextSetRGBFillColor, you also to have call CGContextFillRect. Your final draw(_:) implementation would then look like this with Swift 3:

class CustomView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        guard let ctx = UIGraphicsGetCurrentContext() else { return }

        ctx.setFillColor(red: 1, green: 1, blue: 1, alpha: 0)
        ctx.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 0.5)

        let rectangle = CGRect(x: 0, y: 100, width: 320, height: 100)
        ctx.fill(rectangle)
        ctx.stroke(rectangle)
    }

}

It will provide the rectangle/square by adjusting values of self frame (customized view or subclass of any class which is inherited from UIView) with transparency.

[self.layer setBorderWidth:1.0];
[self.layer setBorderColor:[[UIColor colorWithRed:0.10 green:0.45 blue:0.73 alpha:1.0] CGColor]];
[self.layer setCornerRadius:2.0];
[self.layer setShadowOffset:CGSizeMake(-2, -2)];
[self.layer setShadowColor:[[UIColor lightGrayColor] CGColor]];
[self.layer setShadowOpacity:0.5];

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGRect rectangle = CGRectMake(0, 100, 320, 100);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.0);   //this is the transparent color
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 0.5);
    CGContextFillRect(context, rectangle);
    CGContextStrokeRect(context, rectangle);    //this will draw the border

}

the effect is like this (backgroundColor is blue)

enter image description here


One handy tip ......

Very often, when you need to draw a square

it's easier to just draw a 'thick stripe' .....

    let context = UIGraphicsGetCurrentContext()
    context!.setLineWidth(100)
    context!.setStrokeColor(blah.cgColor)
    context?.move(to: CGPoint(x: 500, y: 200))
    context?.addLine(to: CGPoint(x: 500, y: 300))
    context!.strokePath()

That will draw a square, which runs downwards from 200 to 300.

It's centered on 500 across, and is 100 wide.