How to add subview inside main view in Swift [clos

2019-03-15 08:44发布

问题:

I need advice how to proceed.

How to slightly dim the main view and display some busy indicator, for the duration of of some action, and then remove the dimming?

In Swift language.

Thanks!

UPD: In Objective-C I use earlier something like this:

UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
dimView.backgroundColor = [UIColor blackColor];
dimView.alpha = 0.5f;
dimView.tag = 1111;
dimView.userInteractionEnabled = NO;
[self.view addSubview:dimView];

How this code we can do it in Swift?

回答1:

Do as follows, I have checked, Working Fine In Swift - iOS 8

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 = CGRectMake(0,200,320,200)
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 :)