2

我创建了一系列“半透明”的 UI 元素,使用@IBDesignable其中我将背景颜色设置为白色和低的部分 alpha。

这适用于UIViewUIButton背景保持清晰,文本颜色虽然在运行时正确显示为白色,但在 Xcode (IB) 中显示为默认蓝色。

我在这里做错了什么?如果它适用于UIView我不明白为什么相同的代码不适用于UIButton.

import UIKit

@IBDesignable
class TranslucentButton: UIButton {
    override func drawRect(rect: CGRect) {
        backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.1)
        setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)

        layer.cornerRadius = 4
        layer.masksToBounds = true
        layer.borderColor = UIColor.whiteColor().CGColor
        layer.borderWidth = 1
    }
}

@IBDesignable
class TransulucentUIView: UIView {
    override func drawRect(rect: CGRect) {
        backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.1)

        layer.cornerRadius = 4
        layer.masksToBounds = true
        layer.borderColor = UIColor.whiteColor().CGColor
        layer.borderWidth = 1
    }
}
4

0 回答 0