如何在swift中更改子视图的背景色?
我创建了一个新的子视图类来绘制图形 我已重写函数drawRect(),但无法更改 子视图的背景色我使用了background方法,但它不起作用如何在swift中更改子视图的背景色?,swift,colors,background,Swift,Colors,Background,我创建了一个新的子视图类来绘制图形 我已重写函数drawRect(),但无法更改 子视图的背景色我使用了background方法,但它不起作用 import UIKit class AbedView:UIView { override func drawRect(rect: CGRect) { let color = UIColor.blueColor() // Do any additional setup after loading the vie
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()
}
}
您需要在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()
}
}
您需要在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()
}
}
我创建了一个“prepare”函数,在创建视图时调用该函数:
class myView: UIView {
func prepare() {
backgroundColor = UIColor.blueColor()
}
override func drawRect(rect: CGRect) {
// custom stuff
}
}
用法(返回要在tableview标头中使用的新视图):
我相信有一种更简洁的方法可以做到这一点,但我不想弄乱初始值设定项,创建一个扩展,或者乱搞层。这很简单,也很有效。就我所见,唯一的缺点是您无法在drawrect:time动态确定颜色。但是,如果您在初始化期间知道颜色,这应该可以工作
享受。我创建了一个“prepare”函数,在创建视图时调用该函数:
class myView: UIView {
func prepare() {
backgroundColor = UIColor.blueColor()
}
override func drawRect(rect: CGRect) {
// custom stuff
}
}
用法(返回要在tableview标头中使用的新视图):
我相信有一种更简洁的方法可以做到这一点,但我不想弄乱初始值设定项,创建一个扩展,或者乱搞层。这很简单,也很有效。就我所见,唯一的缺点是您无法在drawrect:time动态确定颜色。但是,如果您在初始化期间知道颜色,这应该可以工作
享受。按照加里·马金的想法,这对我来说很有效。我有一个类
GameBoardView
的视图GameBoardView
,它是UIView
的一个子类
当我这样做时,它不会覆盖我在故事板中设置的颜色:
class GameBoardView: UIView {
override func drawRect(rect: CGRect) {
self.backgroundColor = UIColor.greenColor()
}
}
但是,当我将该行放入ViewController
的viewwillispect()
方法时,它会起作用:
class ViewController: UIViewController {
// some code
override func viewWillAppear(animated: Bool) {
// more code
gameBoard.backgroundColor = UIColor.greenColor()
// some other code
}
// still more code
}
希望它能有所帮助遵循加里·马金的想法,这对我来说很有效。我有一个类
GameBoardView
的视图GameBoardView
,它是UIView
的一个子类
当我这样做时,它不会覆盖我在故事板中设置的颜色:
class GameBoardView: UIView {
override func drawRect(rect: CGRect) {
self.backgroundColor = UIColor.greenColor()
}
}
但是,当我将该行放入ViewController
的viewwillispect()
方法时,它会起作用:
class ViewController: UIViewController {
// some code
override func viewWillAppear(animated: Bool) {
// more code
gameBoard.backgroundColor = UIColor.greenColor()
// some other code
}
// still more code
}
希望它有帮助创建此视图时不必设置背景色 创建视图后,只要在视图上调用这行代码,它的背景色就会改变
view.backgroundColor = UIColor.blackColor()
创建此视图时,不必设置背景色 创建视图后,只要在视图上调用这行代码,它的背景色就会改变
view.backgroundColor = UIColor.blackColor()