首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加为带有UIImageView的UIView中的子视图的clipsToBounds不起作用

添加为带有UIImageView的UIView中的子视图的clipsToBounds不起作用
EN

Stack Overflow用户
提问于 2022-07-04 06:15:06
回答 2查看 127关注 0票数 0

我有一个UIView和一个UIImageView作为子视图,UIImageView是一个重复的纹理。UIView的宽度和高度是正确的,但图像超出了大小。我添加了ClipsToBounds,但它根本没有裁剪图像。是否有特定的顺序,或者我做错了什么,图像没有被剪裁在它的父视图中?

代码语言:javascript
复制
let rectangleView = UIView(frame: CGRect(x: x, y: y, width: width, height: height))
rectangleView.isUserInteractionEnabled = false

if let texturesUrl = layout.Url, let url = texturesUrl.isValidURL() ? URL(string: texturesUrl) : URL(string: String(format: AppManager.shared.baseTexturesUrl, texturesUrl)) {
     let widthLimit = scale * CGFloat(layout.Width ?? 0)
     let heightLimit = scale * CGFloat(layout.Height ?? 0)
     let widthStep = scale * CGFloat(layout.TileWidth ?? layout.Width ?? 0)
     let heightStep = scale * CGFloat(layout.TileHeight ?? layout.Height ?? 0)

     var locY = CGFloat(0)
     let size = CGSize(width: widthStep, height: heightStep)

     if widthLimit > 0, heightLimit > 0 {
          while locY < heightLimit {
              var locX = CGFloat(0)
                  while locX < widthLimit {
                      let imageView = UIImageView()
                      rectangleView.addSubview(imageView)
                      imageView.contentMode = .scaleAspectFill
                      imageView.translatesAutoresizingMaskIntoConstraints = false
                      imageView.clipsToBounds = true
                      imageView.isUserInteractionEnabled = false
                      imageView.anchor(top: rectangleView.topAnchor, leading: rectangleView.leadingAnchor, bottom: nil, trailing: nil, padding: UIEdgeInsets(top: locY, left: locX, bottom: 0, right: 0), size: size)
                      imageView.setImage(with: url, size: size)
                      locX += widthStep
                  }
                  locY += heightStep
              }
          }
     }
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72852420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档