简体   繁体   English

透明视图是黑色的?

[英]Transparent view is black?

I'm trying to create a view that is transparent in certain spots to see an image behind. 我正在尝试创建在某些位置透明的视图以查看后面的图像。 However, for some reason in the transparent part of the view, I'm seeing black, instead of what's behind the view. 但是,由于某种原因,在视图的透明部分中,我看到的是黑色,而不是视图的后面。 I've trimmed it down to very little code and don't understand why my transparent view shows black instead of red (the color of the view behind). 我已经将其精简为很少的代码,并且不明白为什么我的透明视图显示的是黑色而不是红色(后面的视图的颜色)。 Here's my code: 这是我的代码:

class ViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let redView = UIView(frame: view.frame)
        redView.backgroundColor = UIColor.red
        let transparentView = TransparentView(frame: view.frame)

        view.addSubview(redView)
        view.addSubview(transparentView)
    }
}

class TransparentView: UIView {

    override func draw(_ rect: CGRect) {
        UIColor.clear.setFill()
        UIRectFill(rect)
}

I would expect the screen to be full red, but instead it shows full black. 我希望屏幕显示为全红,但显示为全黑。 Before someone says it's a lot easier to make a clear view, I'm actually trying to do more complex things in drawRect, just dropped down to the most basic thing to try to debug my problem. 在有人说清楚视图变得容易得多之前,我实际上是在尝试在drawRect中做更复杂的事情,只是深入到最基本的事情来尝试调试我的问题。 What am I missing here? 我在这里想念什么?

Use self.isOpaque = false; 使用self.isOpaque = false; to make the view/layer transparent even when drawRect is overriden. 使即使覆盖drawRect时视图/图层也透明。

class TransparentView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame);

        self.isOpaque = false;  //Use this..
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        UIColor.clear.setFill()
        UIRectFill(rect)
    }
}

I figured it out. 我想到了。 Apparently even if you override draw , backgroundColor seems to still be considered, and defaults to black. 显然,即使您覆盖draw ,似乎仍会考虑backgroundColor,并且默认为black。 I added the following to my transparent view class: 我在透明视图类中添加了以下内容:

override init(frame: CGRect) {
    super.init(frame: frame)

    backgroundColor = UIColor.clear
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

"drawRect: Implement this method if your view draws custom content. If your view does not do any custom drawing, avoid overriding this method." “ drawRect:如果您的视图绘制自定义内容,请实现此方法。如果您的视图不执行任何自定义绘制,请避免重写此方法。” Link 链接

That been said would be better as you said just set background color on Init. 这样说会更好,因为您说的只是在Init上设置背景色。

override init(frame: CGRect) {
    super.init(frame: frame)

    backgroundColor = UIColor.clear
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Personally i wont subclass a view for so little customization. 就我个人而言,我不会为如此少的自定义子类化视图。 Just set it while creating it. 只需在创建它时进行设置即可。 Also view setup is better on viewDidLoad, not in viewWillAppear. 另外,在viewDidLoad上进行视图设置更好,而不在viewWillAppear中进行设置。 Since it will execute every time your view will go foreground and u will end with two transparent views added. 因为它将在您的视图每次进入前景时执行,并且u将以两个透明视图结尾。 Also keeping those line in a extension with a private function helps to keep your code clear. 另外,使用私有函数将这些行保留在扩展名中有助于保持代码清晰。

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupViews()
    }
}


//MARK: - Private Methods
extension ViewController{
    fileprivate func setupViews(){
       let redView = UIView(frame: view.frame)
       redView.backgroundColor = UIColor.red
       view.addSubview(redView)

       let transparentView = UIView(frame: view.frame)
       transparentView.backgroundColor = UIColor.clear
       view.addSubview(transparentView)
    }
}

Please notice that a more clear approach would be to create those Views in the Storyboard (not by code). 请注意,更清晰的方法是在情节提要中创建这些视图(而不是通过代码)。 Keep code clear and its easier to understand and see whats going on. 保持代码清晰,使其更易于理解,并查看发生了什么。

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

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