Do as follows, I have checked, working fine in Swift
We have have to initialize the view with the frame and then we have to set the .alpha
property for dim the view.
let testFrame = CGRect(x: 0, y: 100, width: 100, height: 100)
var testView : UIView = UIView(frame: testFrame)
testView.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0)
testView.alpha=0.5
self.view.addSubview(testView)
And .addSubview
will add the view inside the main view.
Happy Coding :)