2009-09-11 36 views
11

Tôi có một chế độ xem phụ đã được thêm vào UIView của tôi. Ý tưởng là subview là một bảng điều khiển lơ lửng với uibuttons. Subview được đưa ra một alpha là 0,2, khi người dùng cần để có thể nhìn thấy những gì đằng sau nó.SDK iPhone: Các bản xem phụ không minh bạch trong chế độ xem trong suốt

Vấn đề là khi tôi thêm uibutton vào subview, họ kế thừa alpha từ subview (tôi muốn các nút không trong suốt và có alpha là 1.0). Tôi đã cố gắng giải quyết vấn đề này bằng cách lặp qua các uibutton và thiết lập alpha của chúng trở về 1.0 mà không thành công. Các giải pháp?

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

Trả lời

15

Bạn nên tạo topHoverView với alpha 1.0 và màu nền trong suốt. Sau đó, thêm một subview với một nền đen bao gồm các thông tin đầy đủ với alpha 0.2 và sau đó thêm các nút để 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

Chúc mừng, giải pháp tốt. –

+0

Cảm ơn bạn Philippe, tôi đã cố gắng để làm một điều tương tự, ngoại trừ thông qua IB và tôi đã chỉ không thể có được hiệu quả tôi muốn. Nhưng cách giải quyết của bạn đã giúp tôi có được nó. – sherry

6

Thay vì

topHoverView.backgroundColor = [UIColor transparentColor]; 

, đoạn mã sau là đúng.

topHoverView.backgroundColor = [UIColor clearColor]; 
Các vấn đề liên quan