【问题标题】:UIView not showing after being brought to the frontUIView 被带到前面后不显示
【发布时间】:2017-07-05 15:47:05
【问题描述】:

目前我正在尝试在相机源上画一个圆圈。为此,我创建了另一个 UIView,设置 frame/color/cornerRadius,将其添加到 self.view 的子视图中,然后将圆形 UIView 带到前面。由于某种原因,它没有出现。

我之前已经这样做了,并且我已经编写了与之前完全相同的代码,但它仍然没有显示。

这是我制作视图的地方:

//----Circle View----------
    circleView = UIView(frame: CGRect(x: 150, y: 150, width: 100, height:  100))
    circleView.layer.cornerRadius = self.circleView.frame.width/2
    circleView.layer.borderColor = UIColor.green.cgColor
    circleView.layer.zPosition = 1
    //self.circleView.isHidden = true
    self.view.addSubview(circleView)

这是我把它带到前面的地方:

 if (captureSession?.canAddOutput(dataOutput))!
    {

        captureSession?.addOutput(dataOutput)
        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
        previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
        self.view.layer.addSublayer(previewLayer!)
        self.view.bringSubview(toFront: circleView)
        self.view.bringSubview(toFront: captureButton)
        self.view.bringSubview(toFront: stopButton)

        previewLayer?.frame = self.view.bounds

    }

有没有办法让 circleView 显示?

非常感谢。

【问题讨论】:

  • 使用Debug View Hierarchy ... 它真的存在于层次结构中吗?如果是这样,z 顺序是否有问题,所以它隐藏在某些东西后面?或者,框架是否错误,所以它被放置在视图边界之外?
  • 您可能需要为您的circleView添加约束
  • 你也可以尝试把这行self.view.bringSubview(toFront: circleView)放在最后(previewLayer?.frame = self.view.bounds之后
  • 将您的 previewLayer 的 zPosition 设为 1,并为控件(或在本例中为圆形视图)的 zPosition 设为 2
  • 感谢您的帮助。在 Debug 视图层次结构中,它表示该视图与显示在 previewLayer 上的其他视图处于同一级别。会不会是现有的约束干扰了看到的 circleView?

标签: ios iphone swift uiview


【解决方案1】:

我解决了这个问题。

为了解决这个问题,我向我的故事板添加了一个 UIView,然后为视图控制器创建了一个 IBOutlet。在那之后,景色出现了。我在想这是因为我的故事板中的视图存在现有限制,从而中断了对 circleView 的查看。这是我的猜测。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2014-03-27
    • 1970-01-01
    相关资源
    最近更新 更多