How to add subview inside main view in Swift [closed]

落爺英雄遲暮 提交于 2019-12-20 12:10:21

问题


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

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 :)



来源:https://stackoverflow.com/questions/24859642/how-to-add-subview-inside-main-view-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!