Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将更多项目添加到scrollview顶部时,请向下移动现有项目_Ios_Swift - Fatal编程技术网

Ios 将更多项目添加到scrollview顶部时,请向下移动现有项目

Ios 将更多项目添加到scrollview顶部时,请向下移动现有项目,ios,swift,Ios,Swift,我试图在向下移动所有现有项目的同时,将一个项目添加到scrollview的顶部。我不想删除现有视图并重新添加它们 这就是我构建滚动视图的方式: for var index=0; index < data.users.count; index++ { if data.users[index] as! String != "" { let myImageView:UIImageView = UIImageView() myImageView.image = myIm

我试图在向下移动所有现有项目的同时,将一个项目添加到scrollview的顶部。我不想删除现有视图并重新添加它们

这就是我构建滚动视图的方式:

for var index=0; index < data.users.count; index++
 {
  if data.users[index] as! String != ""
   {
    let myImageView:UIImageView = UIImageView()
    myImageView.image = myImage
    myImageView.contentMode = UIViewContentMode.ScaleAspectFit
    myImageView.frame.size.width = imageWidth
    myImageView.frame.size.height = imageHeight
    myImageView.frame.origin.x = 0
    myImageView.frame.origin.y = MyVariables.yPosition
    self.myScrollView.addSubview(myImageView)

    MyVariables.yPosition += imageHeight + spacer
    MyVariables.scrollViewContentSize+=imageHeight + spacer

    self.myChatScrollView.contentSize = CGSize(width: imageWidth, height: MyVariables.scrollViewContentSize)
}}

为了更好地处理此类内容,您应该使用TableView,当您添加和删除项时,它可以更好地处理所有内容

如果您的要求是使用滚动视图,那么您需要一步一步地进行

  • 首先,需要通过更改帧原点(y)来向下移动项目

    -对所有子视图运行for循环,并使您的图幅y+新图幅的高度

  • 如果使用自动布局,还请确保处理所有约束

  • 如果你能更好地处理你的约束,你真的不需要移动所有的项目,如果你移动到顶部,其余的项目将被处理
  • 完成移动其他项目后,使用动画添加新项目

您需要更新每个子视图帧的原点以向下移动它们。
// on button click, I want to shift everything down and add the new item on top 
// without removing the current views