iPhone SDK : Non-transparent subviews in transparent view

标签 iphone

我有一个 subview 已添加到我的 UIView 中。这个想法是 subview 是一个带有 uibuttons 的悬停面板。 subview 的 alpha 值为 0.2,因为用户需要能够看到其后面的内容。

问题是,当我将 uibuttons 添加到 subview 时,它们从 subview 继承 alpha(我希望按钮不透明并且 alpha 为 1.0)。我试图通过迭代 uibuttons 并将其 alpha 设置回 1.0 来解决这个问题,但没有成功。解决方案?

    // 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;
    }

最佳答案

您应该使用 alpha 1.0 和透明背景色创建 topHoverView。然后添加一个黑色背景的 subview ,用 alpha 0.2 覆盖整个 View ,然后将按钮添加到 topHoverView:

// 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;
}

关于iPhone SDK : Non-transparent subviews in transparent view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1409285/

相关文章:

ios - 带有粘性水平指示器的 UICollectionView Swift 3.0

iphone - 调整 UITextView 的大小

iphone - 如何检测手机应用程序是否适用于使用 iPhone 操作系统的各种设备

iphone - 有没有办法防止 stringWithFormat 四舍五入?

iphone - 如何在iPhone应用程序中实现页面控制?

ios - 快速选择音频设备输出(如 Waze)

iphone - UIButton setTitleColor :forState: question

ios - 什么是 rootViewController 以及何时使用它?

ios - Swift - 如何强制用户在 UITextField 中只导入整数?

iphone - 以编程方式将内容添加到音乐库