【问题标题】:How to change background color of subview in swift?如何快速更改子视图的背景颜色?
【发布时间】:2016-01-15 19:40:24
【问题描述】:

我创建了一个新的子视图类来进行绘图 我已经覆盖了函数 drawRect() 但我无法更改 子视图的背景颜色我使用了背景方法,但它不起作用!

import UIKit

class AbedView:UIView {
     override func drawRect(rect: CGRect) {
        let color = UIColor.blueColor()
        // Do any additional setup after loading the view, typically from a nib.
        let cgrRect = CGRect(x: 0, y: 0, width: 100, height: 100)
        let newView = UIBezierPath(rect: cgrRect)
        print(newView.bounds)
        color.set()
        newView.lineWidth = 5
        newView.stroke()
        self.backgroundColor = UIColor.blackColor()



    }

}

【问题讨论】:

    标签: swift colors background


    【解决方案1】:

    你需要在drawRect之前设置背景颜色。当你的 drawRect 被调用时,背景已经被绘制了。如果你需要能够在drawRect中设置背景颜色,就自己画吧,就像你画蓝色矩形轮廓的方式一样。

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.backgroundColor = UIColor.blackColor()
    }
    
    class AbedView:UIView {
         override func drawRect(rect: CGRect) {
            let color = UIColor.blueColor()
            let cgrRect = CGRect(x: 0, y: 0, width: 100, height: 100)
            let newView = UIBezierPath(rect: cgrRect)
            print(newView.bounds)
            color.set()
            newView.lineWidth = 5
            newView.stroke()
        }
    }
    

    【讨论】:

      【解决方案2】:

      您不必在创建此视图时设置背景颜色。

      视图创建后,只要在视图上调用这行代码,它的背景颜色就会改变

      view.backgroundColor = UIColor.blackColor()
      

      【讨论】:

        【解决方案3】:

        按照 Gary Makin 的想法,这对我有用。我有 gameBoardGameBoardView 的视图,它是 UIView 的子类。

        当我这样做时,它不会覆盖我在情节提要中设置的颜色:

        class GameBoardView: UIView {
          override func drawRect(rect: CGRect) {
            self.backgroundColor = UIColor.greenColor()
          }
        }
        

        但是当我将该行放入 ViewControllerviewWillAppear() 方法中时,它可以工作:

        class ViewController: UIViewController {
          // some code
          override func viewWillAppear(animated: Bool) {
            // more code
            gameBoard.backgroundColor = UIColor.greenColor()
            // some other code
          }
          // still more code
        }
        

        希望对你有帮助

        【讨论】:

          【解决方案4】:

          我创建了一个“准备”函数,在创建视图时调用它:

          class myView: UIView {
            func prepare() {
                backgroundColor = UIColor.blueColor()
            }
          
            override func drawRect(rect: CGRect) {
            // custom stuff
            }
          }
          

          用法(这是返回要在 tableview 标题中使用的新视图):

                  let myView = myView()
                  myView.prepare()
                  return myView
          

          我确信有一种更简洁的方法可以做到这一点,但我不想弄乱初始化程序、创建扩展或乱用层。这很简单并且有效。据我所知,唯一的缺点是您无法在 drawrect: time 动态确定颜色。但是,如果您在初始化期间知道颜色,这应该可以工作。

          享受吧。

          【讨论】:

            猜你喜欢
            • 2018-06-11
            • 1970-01-01
            • 2022-01-25
            • 1970-01-01
            • 1970-01-01
            • 2019-10-19
            • 1970-01-01
            • 2020-05-03
            • 2015-06-02
            相关资源
            最近更新 更多