2009-09-11 5 views
11

Ich habe eine Subview, die zu meinem UIView hinzugefügt wurde. Die Idee ist, dass die Unteransicht ein Schwebepanel mit uibuttons ist. Die Unteransicht erhält ein Alpha von 0,2, da der Benutzer sehen können muss, was sich dahinter befindet.iPhone SDK: Nicht transparente Subviews in transparenter Sicht

Das Problem ist, dass, wenn ich uibuttons der Unteransicht hinzufügen, sie das Alpha aus der Unteransicht erben (ich möchte, dass die Schaltflächen nicht transparent sind und ein Alpha von 1,0 haben). Ich habe versucht, dieses Problem anzugehen, indem ich durch die uibuttons iterierte und ihren Alpha zurück zu 1.0 ohne Erfolg einsetzte. Lösungen?

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

Antwort

15

Sie sollten topHoverView mit Alpha 1.0 und einer transparenten Hintergrundfarbe erstellen. Dann ein Subview mit einem schwarzen Hintergrund hinzufügen, die die komplette Ansicht mit Alpha umfasst 0,2 und fügen Sie dann die Tasten 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

Prost, nette Abhilfe. –

+0

Danke Philippe, ich habe versucht, eine ähnliche Sache zu machen, außer durch das IB und ich konnte einfach nicht den gewünschten Effekt erzielen. Aber deine Workaround half mir, es zu bekommen. – sherry

6

Statt

topHoverView.backgroundColor = [UIColor transparentColor]; 

, ist der folgende Code rechts.

topHoverView.backgroundColor = [UIColor clearColor];