Ios 将UIView添加到UIViewImage上。用户可以缩放图像

Ios 将UIView添加到UIViewImage上。用户可以缩放图像,ios,swift,Ios,Swift,我试图在UIImage上添加UIView。但这对我不起作用。 当我缩小和增大图像时,UIView向下移动,无法完全覆盖我的图像。 我使用双击进行缩放 @objc func doubleTap(gestureRecognizer: UITapGestureRecognizer) { if self.productScrollView.zoomScale == 1 { self.productScrollView.zoom(to: self.zoomRectForScal

我试图在UIImage上添加UIView。但这对我不起作用。 当我缩小和增大图像时,UIView向下移动,无法完全覆盖我的图像。

我使用双击进行缩放

@objc func doubleTap(gestureRecognizer: UITapGestureRecognizer) {

    if self.productScrollView.zoomScale == 1 {
        self.productScrollView.zoom(to: self.zoomRectForScale(scale: self.productScrollView.maximumZoomScale, center: gestureRecognizer.location(in: gestureRecognizer.view)), animated: true)
    } else {
        self.productScrollView.setZoomScale(1, animated: true)
    }

}
我试着使用这些代码,但对我来说不起作用

  • 第一:

     let tintView = UIView()
     tintView.backgroundColor = UIColor(white: 0, alpha: 0.5)
     tintView.frame = CGRect(x: 0, y: 0, width: imageView.frame.width, 
     height: imageView.frame.height)
     imageView.addSubview(tintView)
    
  • 第二:

    let tintView = UIView()
    tintView.backgroundColor = UIColor(white: 0, alpha: 0.5)
    tintView.translatesAutoresizingMaskIntoConstraints = false
    imageView.addSubview(tintView)
    NSLayoutConstraint.activate([
            tintView.bottomAnchor.constraint(equalTo: imageView.bottomAnchor),
            tintView.leadingAnchor.constraint(equalTo: imageView.leadingAnchor),
            tintView.trailingAnchor.constraint(equalTo: imageView.trailingAnchor),
            tintView.topAnchor.constraint(equalTo: imageView.topAnchor),
            ])
    
  • 另外,我尝试在故事板上添加,但它也不适合我。
    有人有想法或建议吗?

    尝试将UIView添加到viewController的主视图,而不是UIImageView,并对imageView设置约束