當我設置按鈕的alpha值時,它也會影響標題的不透明度。有沒有一種方法只定位背景並將標題alpha保留爲1.0?設置UIButton背景的alpha但不是標題
回答
子類的UIButton和擴展的setEnabled:方法似乎工作:
- (void) setEnabled:(BOOL)enabled {
[super setEnabled:enabled];
if (enabled) {
self.imageView.alpha = 1;
} else {
self.imageView.alpha = .25;
}
}
你只是使用的背景素色?或者你在使用圖片嗎?
如果您使用的是圖像,那麼你可以如果你使用一種顏色,那麼你可以設置顏色的Alpha使用圖像內置了阿爾法。
。
但是,更改任何視圖的alpha會影響所有子視圖。
使用UIButton,您可以通過將按鈕樣式設置爲「自定義」而不是「Round Rect」來刪除背景。這使標題保持不變,但刪除了按鈕背景。如果您在Storyboard中執行此操作,則可以更改元素屬性中的按鈕樣式。對於代碼,格式是:
UIButton *button = [[UIButton alloc] buttonWithType:UIButtonTypeCustom];
// Setup frame and add to view
這個工作對我來說:
self.myButton.backgroundColor = [UIColor clearColor];
,或者如果你不想把它徹底清除,但仍與透明度,你可以使用:
self.myButton.backgroundColor = [UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:0.5];
第二個例子會給你alpha 0.5灰色。
爲什麼我不能想到這個?謝謝! – Elsint
在斯威夫特:
import UIKit
class SignInUpMenuTableViewControllerBigButton: UIButton {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.applyCustomDesign()
}
func applyCustomDesign() {
// We set the background color of the UIButton.
self.clearHighlighted()
}
override var highlighted: Bool {
didSet {
if highlighted {
self.highlightBtn()
} else {
self.clearHighlighted()
}
}
}
func highlightBtn() {
self.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0)
}
func clearHighlighted() {
self.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.05)
}
}
加上一個'colorWithAlphaCompnent'!對於Swift 3:'UIColor.black.withAlphaComponent(0.8)' – kakubei
- 1. 如何設置UIButton的背景顏色的alpha值?
- 2. 設置背景顏色,但不是整個背景
- 3. QTableWidget設置標題單元的背景
- 4. 如何設置標題欄的背景?
- 5. 設置UIButton的標題
- 6. 爲UIButton設置一個背景圖像
- 7. 如何設置半透明背景,但不是前景的WP8?
- 8. 爲Panorama標題設置背景顏色
- 9. 工具欄與漸變背景設置標題背景透明
- 10. 爲ios中的uibutton setimage設置alpha值?
- 11. UIButton背景圖像不變
- 12. UIButton背景將不透明
- 13. Alpha Blend如果背景Alpha不是255/1.0f
- 14. 背景Alpha混合
- 15. UIButton沒有正確設置alpha
- 16. 設置菜單背景是不透明
- 17. UIButton的背景圖片看起來不錯的SIM卡,但不能在設備
- 18. 如何設置標題上的UIButton
- 19. 使用自動佈局的背景圖像和標題的UIButton
- 20. 與自定義背景的UIButton失去了標題
- 21. 與更改UIButton背景圖像問題
- 22. 選擇時無法設置UIButton的背景顏色?
- 23. 未被UIImagePicker設置的Swift UIButton背景圖片
- 24. 我如何設置在UIButton圖像前面的UIButton的標題
- 25. 的UIButton - 不需要的背景以及
- 26. UIButton:背景圖像設置按鈕狀態高亮顯示時的問題?
- 27. 在導航欄中用alpha設置背景顏色
- 28. 如何將DefaultComplexItemRenderer背景alpha設置爲0
- 29. UIButton設置圖像和標題
- 30. UIButton獲取錯誤設置標題
這實際上爲工作您?在iOS 7中,我似乎無法改變按鈕圖像的Alpha設置。 – Axeva