【问题标题】:Adding imageview as a subview in scrollview swift ios在scrollview swift ios中添加imageview作为子视图
【发布时间】:2018-08-15 12:56:57
【问题描述】:

我正在向滚动视图添加图像,但只在滚动视图中获取一张图像,我不知道为什么我没有看到任何其他图像,而只是空滚动,除了一张图像

这是我尝试过的 在viewdidload

  imageScroll = UIScrollView(frame: CGRect(x: 10, y: 320, width: 230, height: 250))
  imageScroll.contentSize = CGSize(width: 250, height: 2000)       
  imageScroll.backgroundColor = .white

在函数中

for (b,c) in zip(photoGotArray, 0..<totalPhotosGot) {

   photoDataVarOk = UserDefaults.standard.object(forKey: b) as? NSData
   newImageView = UIImageView(frame: CGRect(x: 10, y: 50, width: 230, height: 200))
   newImageView.viewWithTag(c)
   newImageView.image = UIImage(data: photoDataVarOk! as Data)
   self.imageScroll.addSubview(newImageView)

}

这是只有一张图片和其余空滚动视图的图片

【问题讨论】:

  • 您一遍又一遍地将图像添加到同一个 imageView 中,结果您每次都在替换图像。您需要做的是创建一个imageView 的新实例,并设置一个新框架,该框架低于之前添加的框架。 (你需要做一些数学运算)。

标签: ios swift uiscrollview uiimageview


【解决方案1】:

您需要根据您的代码将 Y 更改为递增,因为所有图像都具有相同的帧

let newImageView = UIImageView(frame: CGRect(x: 10, y: 50 + ( 200 * i ) , width: 230, height: 200))

根据需要或循环参数设置i

//

for (c,b) in photoGotArray.enumerated() {

    photoDataVarOk = UserDefaults.standard.data(forKey: b) 
    let newImageView = UIImageView(frame: CGRect(x: 10, y: 50 + ( 200 * c), width: 230, height: 200))
    newImageView.viewWithTag(c)
    newImageView.image = UIImage(data: photoDataVarOk!)
    self.imageScroll.addSubview(newImageView)
}

【讨论】:

    【解决方案2】:

    让 newImageView = UIImageView() newImageView = UIImageView(frame: CGRect(x: 10, y: yourImageHight * (indexLoop), width: 230, height: 200))

    在你的循环中

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多