简体   繁体   English

无法从黑色更改 UIView 背景颜色

[英]Cant Change UIView Background Color From Black

This is my first time in which I've encountered problems with drawRect.这是我第一次遇到 drawRect 问题。 I have a simple UIView subclass with a path to fill.我有一个简单的 UIView 子类,其中包含要填充的路径。 Everything is fine.一切安好。 The path gets filled properly, but the background remains black no matter what.路径被正确填充,但无论如何背景仍然是黑色的。 What should I do?我该怎么办? My code is listed below:我的代码如下:

var color: UIColor = UIColor.red {didSet{setNeedsDisplay()}}

private var spaceshipBezierPath: UIBezierPath{
    let path = UIBezierPath()
    let roomFromTop: CGFloat = 10
    let roomFromCenter: CGFloat = 7
    path.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
    path.addLine(to: CGPoint(x: bounds.minX, y: bounds.minY+roomFromTop))
    path.addLine(to: CGPoint(x: bounds.midX-roomFromCenter, y: bounds.minY+roomFromTop))
    path.addLine(to: CGPoint(x: bounds.midX-roomFromCenter, y: bounds.minY))
    path.addLine(to: CGPoint(x: bounds.midX+roomFromCenter, y: bounds.minY))
    path.addLine(to: CGPoint(x: bounds.midX+roomFromCenter, y: bounds.minY+roomFromTop))
    path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY+roomFromTop))
    path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
    path.addLine(to: CGPoint(x: bounds.minX, y: bounds.maxY))
    return path
}

override func draw(_ rect: CGRect) {
    color.setFill()
    spaceshipBezierPath.fill()
}

Here is what the view looks like:这是视图的样子: 在此处输入图片说明

I have a simple UIView subclass ... but the background remains black no matter what我有一个简单的 UIView 子类......但无论如何背景仍然是黑色的

Typically that sort of thing is because you have forgotten to set the UIView subclass's isOpaque to false .通常这种事情是因为您忘记将 UIView 子类的isOpaquefalse It is a good idea to do this in the UIView subclass's initializer in order for it to be early enough.在 UIView 子类的初始化程序中执行此操作是一个好主意,以便它足够早。

For example, here I've adapted your code very slightly.例如,在这里我对您的代码进行了很小的调整。 This is the complete code I'm using:这是我正在使用的完整代码:

class MyView : UIView {
    private var spaceshipBezierPath: UIBezierPath{
        let path = UIBezierPath()
        // ... identical to your code
        return path
    }
    override func draw(_ rect: CGRect) {
        UIColor.red.setFill()
        spaceshipBezierPath.fill()
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()        
        let v = MyView(frame:CGRect(x: 100, y: 100, width: 200, height: 200))
        self.view.addSubview(v)
    }
}

Notice the black background:注意黑色背景:

在此处输入图片说明

Now I add these lines to MyView:现在我将这些行添加到 MyView:

override init(frame:CGRect) {
    super.init(frame:frame)
    self.isOpaque = false
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

See the difference that makes?看到有什么不同了吗?

在此处输入图片说明

在我的情况下,我解决的是从背景颜色“无颜色”切换到背景颜色“清晰颜色”

For future seekers....对于未来的寻求者......

I had the same problem but @matt solution didn't work for me.我遇到了同样的问题,但 @matt 解决方案对我不起作用。

In my scenario, I had a UIView (A) behind another UIView (B).在我的场景中,我在另一个 UIView (B) 后面有一个 UIView (A)。 UIView (A) had a background color and UIView (B) was transparent. UIView (A) 有背景色,UIView (B) 是透明的。 Worth to note that B was subclassed to create a custom shape using UIBezier by overriding draw method.值得注意的是, B 被子类UIBezier通过覆盖draw方法使用UIBezier创建自定义形状。

Turned out that in this situation a transparent UIView means nothing and I had to set the same background color for B or either remove background color of A.原来在这种情况下,透明的 UIView 没有任何意义,我必须为 B 设置相同的背景颜色或删除 A 的背景颜色。

there was no other way, so don't waste your time if you have the same scenario!没有其他办法,所以如果您有相同的情况,请不要浪费时间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM