create a tri-colour view with the colours of the french flag in swift

To create a tri-colour view with the colours of the French flag in Swift, you can use a UIView and set its backgroundColor property to a UIColor created from a CIColor that represents the desired colour.

Here's an implementation of this solution:

main.swift
let triColourView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    
let blueColor = UIColor.init(ciColor: CIColor(red: 0, green: 85/255, blue: 164/255))
let whiteColor = UIColor.init(ciColor: CIColor.white)
let redColor = UIColor.init(ciColor: CIColor(red: 239/255, green: 65/255, blue: 53/255))
    
let firstThird = CGRect(x: 0, y: 0, width: triColourView.frame.width/3, height: triColourView.frame.height)
let secondThird = CGRect(x: triColourView.frame.width/3, y: 0, width: triColourView.frame.width/3, height: triColourView.frame.height)
let thirdThird = CGRect(x: triColourView.frame.width/3 * 2, y: 0, width: triColourView.frame.width/3, height: triColourView.frame.height)
    
let blueView = UIView(frame: firstThird)
blueView.backgroundColor = blueColor
triColourView.addSubview(blueView)
    
let whiteView = UIView(frame: secondThird)
whiteView.backgroundColor = whiteColor
triColourView.addSubview(whiteView)

let redView = UIView(frame: thirdThird)
redView.backgroundColor = redColor
triColourView.addSubview(redView)
1049 chars
22 lines

Here we have created a new UIView called triColourView with a frame of 100x100. We then create the UIColors for the blue, white, and red colours of the French flag using CIColor.

Next, we divide the triColourView into three equal parts using CGRects, and create a UIView for each segment with the appropriate background colour, and add them to the triColourView.

Finally, we have a new UIView called triColourView which now displays the French flag in tri-colour view!

gistlibby LogSnag