简体   繁体   English

iPhone SDK:透明视图中的非透明子视图

[英]iPhone SDK: Non-transparent subviews in transparent view

I have a subview that has been added to my UIView. 我有一个已添加到我的UIView的子视图。 The idea is that the subview is a hovering panel with uibuttons. 想法是子视图是带有uibutton的悬停面板。 The subview is given an alpha of 0.2, as the user needs to be able to see what is behind it. 子视图的alpha值为0.2,因为用户需要能够看到其背后的内容。

The problem is that when I add uibuttons to the subview, they inherit the alpha from the subview (I want the buttons to be non-transparent and have an alpha of 1.0). 问题是,当我将uibuttons添加到子视图时,它们从子视图继承了alpha(我希望按钮是不透明的,并且alpha为1.0)。 I've tried to tackle this problem by iterating through the uibuttons and setting their alpha back to 1.0 without success. 我试图通过遍历uibutton并将其alpha设置回1.0来解决此问题,但没有成功。 Solutions? 解决方案?

    // Create the subview and add it to the view
    topHoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    topHoverView.backgroundColor = [UIColor blackColor];
    [topHoverView setAlpha:0.2];
    [self.view addSubview:topHoverView];

    // Add button 1
    button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button1 setFrame:CGRectMake(20, 10, 80, 30)];
    [button1 setTitle:@"New" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
    [topHoverView addSubview:button1];

    // Add button 2
    button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button2 setFrame:CGRectMake(120, 10, 80, 30)];
    [button2 setTitle:@"Clear" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(button2Action:) forControlEvents:UIControlEventTouchUpInside];
    [topHoverView addSubview:button2];

    // Add button 3
    button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button3 setFrame:CGRectMake(220, 10, 80, 30)];
    [button3 setTitle:@"Delete" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(button3Action:) forControlEvents:UIControlEventTouchUpInside];
    [topHoverView addSubview:d button3];

    // Attempt to iterate through the subviews (buttons) to set their transparency back to 1.0
    for (UIView *subView in topHoverView.subviews) {
        subView.alpha = 1.0;
    }

You should create topHoverView with alpha 1.0 and a transparent background color. 您应该使用alpha 1.0和透明的背景色创建topHoverView。 Then add a subview with a black background that covers the complete view with alpha 0.2 and then add the buttons to topHoverView: 然后添加一个黑色背景的子视图,该子视图用alpha 0.2覆盖整个视图,然后将按钮添加到pHoverView:

// Create the subview and add it to the view
topHoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
topHoverView.backgroundColor = [UIColor transparentColor];
[topHoverView setAlpha:1.0];
[self.view addSubview:topHoverView];

canvasView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
canvasView.backgroundColor = [UIColor blackColor];
[canvasViewView setAlpha:0.2];
[topHoverView.view addSubview:canvasView];

// Add button 1
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(20, 10, 80, 30)];
[button1 setTitle:@"New" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:button1];

// Add button 2
button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setFrame:CGRectMake(120, 10, 80, 30)];
[button2 setTitle:@"Clear" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(button2Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:button2];

// Add button 3
button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button3 setFrame:CGRectMake(220, 10, 80, 30)];
[button3 setTitle:@"Delete" forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(button3Action:) forControlEvents:UIControlEventTouchUpInside];
[topHoverView addSubview:d button3];

// Attempt to iterate through the subviews (buttons) to set their transparency back to 1.0
for (UIView *subView in topHoverView.subviews) {
    subView.alpha = 1.0;
}

Instead of 代替

topHoverView.backgroundColor = [UIColor transparentColor];

, the following code is right. ,以下代码正确。

topHoverView.backgroundColor = [UIColor clearColor];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM