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

后端 未结 5 385
一生所求
一生所求 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 19:55

    This can be easily accomplished with a UITextView and a UIButton. Simply place the UITextView on the screen with the content you want to display, making it the full frame size of the screen, and change the background color to a black background with background alpha of .6

    [UIColor colorWithRed: 0 withGreen: 0 withBlue: 0 withAlpha: .6];
    

    Then place the button as a subview on top, making it the full frame of the screen, and setting it's alpha to 0. Set the action of the button to hide the textview and button.

    Example:

    UITextView* textview = [[UITextView alloc] initWithFrame:self.view.frame];
    [textview setText: @"Text here"];
    [textview setBackgroundColor: [UIColor colorWithRed: 0 withGreen: 0 withBlue: 0 withAlpha: .6]];
    [textview setTextColor: [UIColor whiteColor]];
    [self.view addSubview: textview];
    
    UIButton* btn = [[UIButton alloc] initWithFrame:self.view.frame];
    [btn setAlpha:0];
    [btn addTarget:self action:@selector(method) forEvent:UIControlEventTouchUpInside];
    [self.view addSubview: btn];
    

    You may want to check the addTarget: method; I'm not sure those are the correct parameters off the top of my head.

提交回复
热议问题