Ios drawRect形状周围的黑框
我试图学习核心图形,但在使用drawRect时遇到了这个问题。(下图) 我创建了一个从UIView继承的类,UIView包含drawRect代码。在viewcontroller类中,我设置了尺寸并添加了子视图 结果得到的图像在我的形状周围有一个黑框。我怎样才能解决这个问题Ios drawRect形状周围的黑框,ios,swift,core-graphics,drawrect,Ios,Swift,Core Graphics,Drawrect,我试图学习核心图形,但在使用drawRect时遇到了这个问题。(下图) 我创建了一个从UIView继承的类,UIView包含drawRect代码。在viewcontroller类中,我设置了尺寸并添加了子视图 结果得到的图像在我的形状周围有一个黑框。我怎样才能解决这个问题 class myViewController: UIViewController { var bg = Background() override func viewDidLoad() { super.viewDi
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()
}
}