首页 > 解决方案 > 在 UIViewImage 上添加 UIView。用户可以缩放图像

问题描述

我试图在我的 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)
    }

}

我尝试使用这些代码,但它对我不起作用。

  1. 第一的:

     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)
    
  2. 第二:

    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),
            ])
    

另外,我尝试添加情节提要,但它对我也不起作用。有没有人有想法或建议?

标签: iosswift

解决方案


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


推荐阅读