如何正确使用 UIView 和 drawRect() 在 playground 中自定义它?
How to work properly with UIView and drawRect() to customize it in the playground?
为什么在 playground 函数 drawRect() 中当我想覆盖它并显示视图时不起作用?
我尝试这样做:
class CustomView: UIView {
override func drawRect(rect: CGRect) {
//some code to change self properties
}
}
let rect = CGRect(x: 0.0, y: 0.0, width: 300.0, height: 300.0)
let view = CustomView(frame: rect)
view
然后我尝试更改一些属性(例如:backgroundColor)。但是它们没有应用。
我发现了下一个案例:
要在 drawRect 中应用设置的背景颜色,我需要这样调用:
view.setNeedsDisplay()
但是:来自文档:
For example, you do not need to override this method if your view just
displays a background color or if your view sets its content directly
using the underlying layer object.
为什么在 playground 函数 drawRect() 中当我想覆盖它并显示视图时不起作用? 我尝试这样做:
class CustomView: UIView {
override func drawRect(rect: CGRect) {
//some code to change self properties
}
}
let rect = CGRect(x: 0.0, y: 0.0, width: 300.0, height: 300.0)
let view = CustomView(frame: rect)
view
然后我尝试更改一些属性(例如:backgroundColor)。但是它们没有应用。
我发现了下一个案例:
要在 drawRect 中应用设置的背景颜色,我需要这样调用:
view.setNeedsDisplay()
但是:来自文档:
For example, you do not need to override this method if your view just displays a background color or if your view sets its content directly using the underlying layer object.