【问题标题】:How do I make a semi-transparent view layer above a current view?如何在当前视图上方制作半透明视图层?
【发布时间】:2013-04-29 16:11:13
【问题描述】:

您可能以前见过这种情况,它在 ScoutMob 等消费时尚应用中变得非常流行。我正在尝试在启动时实现 60% 的透明视图,它将覆盖我的主屏幕,解释如何使用主屏幕的功能并在点击时消失。

我已经完成了我的整个应用程序(它从几年前开始就使用 .xib,但也可以随意以故事板格式解释,因为我可能会在其他 iOs 5.0+ 应用程序中重复使用此功能。)

制作单个视图没有问题,但暂时将一个视图叠加在另一个视图之上是我直觉上没有想到的。我将继续研究并包括我发现的任何有用的提示,以防其他人尝试做同样的事情。

【问题讨论】:

标签: ios objective-c uikit


【解决方案1】:
// 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];

完成后,您可以将其删除:

[coverView removeFromSuperview];

Swift3 版本:

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

删除它:

coverView.removeFromSuperview()

【讨论】:

    【解决方案2】:

    这可以通过 UITextView 和 UIButton 轻松完成。只需将 UITextView 与您要显示的内容放在屏幕上,使其成为屏幕的全帧大小,并将背景颜色更改为背景 alpha 为 0.6 的黑色背景

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

    然后将按钮作为子视图放在顶部,使其成为屏幕的全帧,并将其alpha设置为0。将按钮的操作设置为隐藏textview和按钮。

    例子:

    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];
    

    您可能需要检查 addTarget: 方法;我不确定这些参数是否正确。

    【讨论】:

      【解决方案3】:

      只需建立一个机制来确定它是否是应用程序的第一次启动,然后告诉您的主视图控制器在当前视图之上添加一个(可能是图像)视图,其 alpha 为 0.6

      if (figureOutIfIsFirstLaunch)
      {
          UIImageView *myOverlay = [...];
          myOverlay.frame = self.view.bounds;
          myOverlay.alpha = 0.6;
          [self.view addSubview:myOverlay];
      } 
      

      【讨论】:

      • 将其放入我的 applicationDelegate 的 application: didFinishLaunchingWithOptions: 而不是包含视图的控​​制器会有什么影响吗?
      【解决方案4】:
      1. 创建UIView,命名为你想要的(dimBackgroundUIView)然后设置 背景颜色为黑色,并将 alpha 设置为 0.1 或 0.2 或....
      2. 需要调暗的时候添加这两行代码 背景:[self.view addSubview:dimBackgroundUIView]; [self addConstraintsForDimView];
      3. 并在您想要移除调光时添加这两行代码 背景:if(dimBackgroundUIView) [dimBackgroundUIView removeFromSuperview];
      4. 不要忘记添加约束(布局) dimBackgroundUIView

      检查一下: iOS Dim background when a view is shown

      并且不要忘记阅读代码下方的注释以了解您必须做什么。

      【讨论】:

        【解决方案5】:

        Swift 4 更新

         view.isOpaque = false  
        
         view.backgroundColor = UIColor.black.withAlphaComponent(0.2)
        

        这个选项在 storyboard 中可用。 确保取消选中不透明选项

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-29
          • 1970-01-01
          • 2021-01-19
          • 1970-01-01
          • 2015-04-21
          • 2021-03-22
          • 2014-09-19
          相关资源
          最近更新 更多