Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.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

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 如何在addSubview中添加类UIView_Ios_Swift_Xcode - Fatal编程技术网

Ios 如何在addSubview中添加类UIView

Ios 如何在addSubview中添加类UIView,ios,swift,xcode,Ios,Swift,Xcode,我有一个绘制椭圆的代码,但它位于从UIView继承的单独类中 class DRAW: UIView { override func draw(_ rect: CGRect) { var path = UIBezierPath() path = UIBezierPath(ovalIn: CGRect(x: 36.62, y: 77.54, width: 303.19, height: 495.93)) UIColor.green.

我有一个绘制椭圆的代码,但它位于从UIView继承的单独类中

    class DRAW: UIView {

    override func draw(_ rect: CGRect) {

        var path = UIBezierPath()
        path = UIBezierPath(ovalIn: CGRect(x: 36.62, y: 77.54, width: 303.19, height: 495.93))
        UIColor.green.setFill()
        path.stroke()
        path.fill()
        
    }
}

如果我试图通过AddiSVIEW()添加它,那么它不在屏幕上,如果我给它尺寸,那么整个显示的黑色爬行,只有中间的是我需要的椭圆。

 view.addSubview(DRAW.init(CGRect(x: 36.62, y: 77.54, width: 303.19, height: 495.93)))

如何仅显示没有黑色正方形的椭圆。非常感谢您的帮助

您只需设置视图的清晰背景色即可

let yourView = DRAW.init(frame: CGRect(x: 36.62, y: 77.54, width: 303.19, height: 495.93))
yourView.backgroundColor = .clear
self.view.addSubview(yourView)

您似乎在理解坐标系时遇到了一些困难<代码>绘制和视图具有不同的坐标系<代码>(x:36.62,y:77.54,宽度:303.19,高度:495.93)没有意义,除非它位于坐标系中。这些坐标是相对于什么点的?不太明白您添加了什么作为子视图?您希望椭圆在哪里?我知道它是x:36.62,y:77.54,但这意味着x方向的36.62点(从某个位置开始),y方向的77.54点(从某个位置开始)。这里的“某个位置”是什么?你没有在你的问题中指定,我怀疑你没有想过这个问题。根据iPhone 11 Pro上的这些坐标,它应该在屏幕中间。