【问题标题】:Why do all the subviews of a UIView show translucency when the UIView's alpha is set less than 1.0?当 UIView 的 alpha 设置小于 1.0 时,为什么 UIView 的所有子视图都显示半透明?
【发布时间】:2014-05-15 10:53:58
【问题描述】:

单击UIButton 时,我会创建一个UIView 并将其放在前面。我将它的“alpha=0.6”设置为显示半透明,然后我添加了各种“子视图”。不知何故,这些“子视图”也显示了我不想要的半透明。谢谢!

- (IBAction)OnClickTutorialGuideButton:(id)sender
{
    dimView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
    dimView.backgroundColor = [UIColor blackColor];
    dimView.alpha = 0.4;
    [self.view addSubview:dimView];
    [self.view bringSubviewToFront:dimView];

    [UIView animateWithDuration:0.3
                     animations:^{
                         dimView.alpha = 0.6;
                     }];

    // Label
    UILabel *getStartedLabel=[[UILabel alloc]initWithFrame:CGRectMake(72, 50, 176, 25)];
    getStartedLabel.text=@"Getting Started";
    getStartedLabel.textColor=[UIColor colorWithRed:(243.0/255.0) green:(107.0/255.0) blue:(55.0/255.0) alpha:1.0];
    getStartedLabel.font=[UIFont boldSystemFontOfSize:25.0f];
    [dimView addSubview:getStartedLabel];

    // ImageView
    UIImageView *remImage=[[UIImageView alloc]initWithFrame:CGRectMake(68, 90, 180, 180)];
    remImage.image=[UIImage imageNamed:@"getting_started1_large_icon.png"];
    [dimView addSubview:remImage];
}

a) 之前:

b) 之后:

【问题讨论】:

    标签: ios objective-c uiview transparency


    【解决方案1】:

    这里来自苹果的View and Window Architecture documentation:

    除了提供自己的内容外,视图还可以充当 其他视图的容器。当一个视图包含另一个视图时, 在两个视图之间创建父子关系。孩子 关系中的视图称为子视图和父视图 被称为超级视图。建立这种关系 对应用程序的视觉外观都有影响 以及应用程序的行为。

    因此,当您设置dimView.alpha = 0.4;(这是您的超级视图)时,它也会自动更改其子视图的不透明度。因为您的超级视图包含由 Core Animation 绘制的那些子视图和底层。

    如果您只想更改超级视图的不透明度,可以通过以下方式进行:

    [dimView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]]; 
    

    另见:

    【讨论】:

      【解决方案2】:

      发生这种情况是因为您正在设置子视图的整体容器视图的 alpha。您应该尝试将其背景颜色设置为所需的 alpha 值,以实现您正在寻找的内容。

      【讨论】:

        【解决方案3】:

        从某种意义上说,视图首先是图形上下文。 子视图存在于该上下文中,并且它们自己的上下文相对于同一视图层次结构中它们上方的上下文。 要获得不同的东西,您需要 alpha 小于 1.0 的视图之外的视图

        【讨论】:

          猜你喜欢
          • 2011-09-01
          • 1970-01-01
          • 2017-01-12
          • 1970-01-01
          • 2021-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-21
          相关资源
          最近更新 更多