簡體   English   中英

iPhone SDK:透明視圖中的非透明子視圖

[英]iPhone SDK: Non-transparent subviews in transparent view

我有一個已添加到我的UIView的子視圖。 想法是子視圖是帶有uibutton的懸停面板。 子視圖的alpha值為0.2,因為用戶需要能夠看到其背后的內容。

問題是,當我將uibuttons添加到子視圖時,它們從子視圖繼承了alpha(我希望按鈕是不透明的,並且alpha為1.0)。 我試圖通過遍歷uibutton並將其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。 然后添加一個黑色背景的子視圖,該子視圖用alpha 0.2覆蓋整個視圖,然后將按鈕添加到pHoverView:

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

代替

topHoverView.backgroundColor = [UIColor transparentColor];

,以下代碼正確。

topHoverView.backgroundColor = [UIColor clearColor];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM