2013-09-16 38 views
2

我有一個iphone應用程序中的自定義視圖,當條件滿足時,應該調暗屏幕並向用戶顯示一些輸入字段。iOS UIButton始終是透明的

我沒有問題禁用主控制和「調光」屏幕(只是一個UIView與alpha = 0.6),但是我在這上面顯示的控件總是似乎有一些透明度(我可以閱讀一些通過UIButton的文本),即使我將控件的alpha設置爲1.0並設置opaque = YES。我甚至嘗試在按鈕和覆蓋層之間放置一個額外的不透明層,它仍然是部分透明的。

作爲參考:(iOS的6.1)

UIView * overlay = [[UIView alloc] initWithFrame:parentView.frame]; 
overlay.backgroundColor = [UIColor blackColor]; 
overlay.alpha=0.6; 

UIButton * button = [UIButton buttonWithType:UIButtonRoundedRect]; 
button.backgroundColor = [UIColor whiteColor]; 
button.alpha = 1.0; 
button.opaque = YES; 
[button setTitle:@"done" forState:UIControlStateNormal]; 
[button setFrame:CGRectMake(0.0,0.0,44.0,44.0)]; 

[overlay addSubview:button]; 
[parentView addSubview:overlay]; 

甚至與上面的代碼的按鈕是透明的。有誰知道爲什麼以及如何使按鈕不透明?

+0

你的意思是說你得到的按鈕的alpha也是0.6。我是對吧? –

回答

4

您可以部分看到UIButton的原因是因爲它是覆蓋UIView的子視圖,其alpha爲0.6。你需要做這樣的事情:

// Create the overlay view just like you have it... 
UIView *overlay = [[UIView alloc] initWithFrame:parentView.frame]; 
overlay.backgroundColor = [UIColor blackColor]; 
overlay.alpha = 0.6; 

// Continue adding this to the parent view 
[parentView addSubview:overlay]; 

// Create the button 
UIButton *button = [UIButton buttonWithType:UIButtonRoundedRect]; 
button.backgroundColor = [UIColor whiteColor]; 
[button setTitle:@"Done" forState:UIControlStateNormal]; 
[button setFrame:CGRectMake(0.0, 0.0, 44.0, 44.0)]; 

// Add this button directly to the parent view 
[parentView addSubview:button]; 
+0

謝謝,我一看到它就顯得非常愚蠢。 – James

1

我推薦使用不透明的功能,您將獲得的性能,這是最好的實踐:

  • 使得按鈕不透明的使用IB:選擇所需的按鈕和公用事業右側欄去屬性督察並在查看部分標記不透明,不要忘記更改背景。

  • 之後,你將不得不以編程方式更改不透明的UIButton的內側的標籤:

    yourButtonOutlet.titleLabel.opaque = true; 
    yourButtonOutlet.titleLabel.backgroundColor = [UIColor *YOUR DESIRED COLOR*]; 
    

瞭解,使用不透明是由apple的快速渲染建議。