Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios drawRect形状周围的黑框_Ios_Swift_Core Graphics_Drawrect - Fatal编程技术网

Ios drawRect形状周围的黑框

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

我试图学习核心图形,但在使用drawRect时遇到了这个问题。(下图)

我创建了一个从UIView继承的类,UIView包含drawRect代码。在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()
  }
}