简体   繁体   English

如何快速更改子视图的背景颜色?

[英]How to change background color of subview in swift?

I created a new subview class to make drawing I have override the function drawRect() but i can't change the background color of the subview i used background method but it did'n work ! 我创建了一个新的子视图类以使绘图具有覆盖函数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()



    }

}

You need to set the background color before drawRect. 您需要在drawRect之前设置背景颜色。 By the time your drawRect is called, the background has already been drawn. 在调用drawRect时,背景已经绘制完毕。 If you need to be able to set the background color in the drawRect, just draw it yourself, just like the way you are drawing the blue rectangle outline. 如果需要能够在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()
    }
}

You don't have to set the background color when you create this view. 创建此视图时,无需设置背景颜色。

Anytime after the view is created, simply call this line of code on the view and it's background color will be changed 创建视图后的任何时间,只需在视图上调用此行代码,其背景颜色就会更改

view.backgroundColor = UIColor.blackColor()

Following along Gary Makin's idea, this worked for me. 遵循Gary Makin的想法,这对我有用。 I have a view gameBoard of class GameBoardView which is a subclass of UIView . 我有一个类GameBoardView的view gameBoard ,它是UIView的子类。

When I did it this way it does not override the colour I set in the storyboard: 当我这样做时,它不会覆盖我在情节提要中设置的颜色:

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

But when I put that line in the ViewController 's viewWillAppear() method it works: 但是,当我将该行放入ViewControllerviewWillAppear()方法中时,它将起作用:

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

Hope it helps 希望能帮助到你

I created a "prepare" function, which I call when the view is created: 我创建了一个“准备”函数,该函数在创建视图时调用:

class myView: UIView {
  func prepare() {
      backgroundColor = UIColor.blueColor()
  }

  override func drawRect(rect: CGRect) {
  // custom stuff
  }
}

Usage (this is returning the new view to use in a tableview header): 用法(这将返回要在tableview标头中使用的新视图):

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

I'm sure there's a more concise way to do this, but I didn't want to mess with the initializers, create an extension, or muck around with layers. 我敢肯定,有一种更简洁的方法可以做到这一点,但是我不想弄乱初始化程序,创建扩展或弄乱层。 This is simple and works. 这很简单并且有效。 The only downside as far as I can see is that you can't dynamically determine the color at drawrect: time. 据我所知,唯一的缺点是您不能在drawrect:时间动态确定颜色。 But if you know the color during initialization this should work. 但是,如果您知道初始化期间的颜色,则应该可以使用。

Enjoy. 请享用。

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

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