黑框周围drawRect形状swift
问题描述:
我想学习Core Graphics,并且在使用drawRect时遇到此问题。 (图片如下)黑框周围drawRect形状swift
我做了一个继承自具有drawRect代码的UIView的类。从我的viewcontroller类,我比设置尺寸和添加子视图。
结果是在我的形状周围形成黑色框。我怎样才能解决这个问题?
class myViewController: UIViewController {
var bg = Background()
override func viewDidLoad() {
super.viewDidLoad()
bg = Background(frame: CGRectMake(50, 50, 50, 50))
self.view.addSubview(bg)
self.view.sendSubviewToBack(bg)
}
class Background: UIView {
override func drawRect(rect: CGRect) {
// Drawing code
var path = UIBezierPath(ovalInRect: rect)
UIColor.greenColor().setFill()
path.fill()
}
}
答
您需要设置bg
元素的背景颜色,只需添加此行bg.backgroundColor = UIColor.clearColor()
。因此,在您viewDidLoad方法,更新代码:
bg = Background(frame: CGRectMake(50, 50, 50, 50))
bg.backgroundColor = UIColor.clearColor()
self.view.addSubview(bg)
self.view.sendSubviewToBack(bg)
答
您可以设置矩形的背景色
class Background: UIView {
override func drawRect(rect: CGRect) {
UIColor.whiteColor().setFill()
UIRectFill(rect)
let path = UIBezierPath(ovalInRect: rect)
UIColor.greenColor().setFill()
path.fill()
}
}