gpt4 book ai didi

ios - 为什么 UIView 的 alpha 设置小于 1.0 时,UIView 的所有 subview 都显示半透明?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:11 24 4
gpt4 key购买 nike

单击 UIButton 时,我创建了一个 UIView 并将其置于最前面。我将其“alpha=0.6”设置为显示半透明,然后向其添加各种“ subview ”。不知何故,这些“ subview ”也显示出我不想要的半透明性。谢谢!

- (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) 之前:

enter image description here

b) 之后:

enter image description here

最佳答案

这里来自Apple的View and Window Architecture documentation:

In addition to providing its own content, a view can act as a container for other views. When one view contains another, a parent-child relationship is created between the two views. The child view in the relationship is known as the subview and the parent view is known as the superview. The creation of this type of relationship has implications for both the visual appearance of your application and the application’s behavior.

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

如果您只想更改 super View 的不透明度,您可以通过以下方式进行:

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

另见:

关于ios - 为什么 UIView 的 alpha 设置小于 1.0 时,UIView 的所有 subview 都显示半透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23676184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com