How do I make a semi-transparent view layer above a current view?

后端 未结 5 373
一生所求
一生所求 2021-02-01 19:28

You\'ve likely seen this before, its become exceedingly popular in consumery chic apps like ScoutMob. I\'m trying to implement a 60% transparent view on launch that will cover m

5条回答
  •  遥遥无期
    2021-02-01 20:03

    // get your window screen size
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    //create a new view with the same size
    UIView* coverView = [[UIView alloc] initWithFrame:screenRect];
    // change the background color to black and the opacity to 0.6
    coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    // add this new view to your main view
    [self.view addSubview:coverView];
    

    when you are done with it , you can remove it :

    [coverView removeFromSuperview];
    

    Swift3 version:

    // get your window screen size
    let screenRect = UIScreen.main.bounds
    //create a new view with the same size
    let coverView = UIView(frame: screenRect)
    // change the background color to black and the opacity to 0.6
    coverView.backgroundColor = UIColor.black.withAlphaComponent(0.6)        
    // add this new view to your main view
    self.view.addSubview(coverView)
    

    to remove it:

    coverView.removeFromSuperview()
    

提交回复
热议问题