iPhone SDK:透明视图中的非透明子视图 [英] iPhone SDK: Non-transparent subviews in transparent view

查看:72
本文介绍了iPhone SDK:透明视图中的非透明子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已添加到我的UIView的子视图。这个想法是子视图是一个带有uibutton的悬停面板。子视图的alpha值为0.2,因为用户需要能够看到它背后的内容。

I have a subview that has been added to my UIView. The idea is that the subview is a hovering panel with uibuttons. The subview is given an alpha of 0.2, as the user needs to be able to see what is behind it.

问题在于,当我向子视图添加uibuttons时,它们会从子视图继承alpha(我希望按钮不透明,alpha值为1.0 )。我试图通过迭代uibuttons并将其alpha设置回1.0而没有成功来解决这个问题。解决方案?

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). I've tried to tackle this problem by iterating through the uibuttons and setting their alpha back to 1.0 without success. 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;
    }


推荐答案

你应该用alpha创建topHoverView 1.0和透明的背景颜色。然后添加一个黑色背景的子视图,用alpha 0.2覆盖整个视图,然后将按钮添加到topHoverView:

You should create topHoverView with alpha 1.0 and a transparent background color. Then add a subview with a black background that covers the complete view with alpha 0.2 and then add the buttons to 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:透明视图中的非透明子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆