2009-09-11 9 views
11

tengo una vista secundaria que se ha añadido a mi UIView. La idea es que la subvista sea un panel flotante con uibutones. La subvista recibe un alfa de 0,2, ya que el usuario necesita poder ver qué hay detrás.iPhone SDK: subvistas no transparentes en vista transparente

El problema es que cuando agrego uibuttons a la subvista, heredan el alfa de la subvista (quiero que los botones sean no transparentes y tengan un alfa de 1.0). Intenté resolver este problema iterando a través de los uibutones y restableciendo su alfa a 1.0 sin éxito. Soluciones?

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

Respuesta

15

Debe crear topHoverView con alpha 1.0 y un color de fondo transparente. A continuación, añadir una vista secundaria con un fondo negro que cubre la visión completa con alfa 0,2 y luego añadir los botones para 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; 
} 
+0

Saludos, buena solución. –

+0

Gracias Felipe, que estaba tratando de hacer una cosa similar, excepto a través del IB y que era simplemente no poder conseguir el efecto que quería. Pero tu solución me ayudó a conseguirlo. – sherry

6

En lugar de

topHoverView.backgroundColor = [UIColor transparentColor]; 

, el siguiente código es correcto.

topHoverView.backgroundColor = [UIColor clearColor]; 
Cuestiones relacionadas