如何在子视图正常的情况下获得半透明视图

发布于 2025-01-08 10:21:29 字数 692 浏览 0 评论 0原文

我有一个 UIViewController 派生类,我想做的是有一个不透明的子视图,而其父视图(主 UIViewController.view)具有 50% 的透明度。

在控制器类的 viewDidLoad 上,我设置了视图和子视图的 alpha 值。但是,我看到的是基本视图和子视图都是半透明的。有没有办法不发生这种情况?

// Class declaration
@interface MyViewControllerClass : UIViewController
{ }
@property (nonatomic, strong) IBOutlet UIView  *contentView;
@end

// Class implementation
- (void)viewDidLoad
{
    // I want the main view to be a transparent overlay
    [self.view setBackgroundColor:[UIColor lightGrayColor]];
    [self.view setAlpha:0.5];

    // This is the subview I want to look normal
    [self.contentView setAlpha:1.0];
    [self.contentView setOpaque:NO];

    [super viewDidLoad];
}

I have a UIViewController derived class and what I'd like to do is have a subview that is not transparent while its parent view (the main UIViewController.view) has 50% transparency.

On the controller classes' viewDidLoad, I set the alpha value of the view and subview. However, what I'm seeing is that both the base view and the subview are both semi-transparent. Is there a way to not have this happen?

// Class declaration
@interface MyViewControllerClass : UIViewController
{ }
@property (nonatomic, strong) IBOutlet UIView  *contentView;
@end

// Class implementation
- (void)viewDidLoad
{
    // I want the main view to be a transparent overlay
    [self.view setBackgroundColor:[UIColor lightGrayColor]];
    [self.view setAlpha:0.5];

    // This is the subview I want to look normal
    [self.contentView setAlpha:1.0];
    [self.contentView setOpaque:NO];

    [super viewDidLoad];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生百味 2025-01-15 10:21:29

你不能那样做。您需要以另一种方式安排您的视图才能获得您想要的效果。尝试将另一个 UIView 添加到主视图并将其透明度设置为 50%。这样您的其他项目就可以保留在主视图上而不会受到影响。

You can't do that. You need to arrange your views in another way to get the effect you desire. Try adding another UIView to the main view and set its transparency to 50% instead. That way your other items can stay on the main view and not be affected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文