微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

以编程方式将视图水平添加到 UIScrollView

如何解决以编程方式将视图水平添加到 UIScrollView

我正在尝试将三个相同的图像视图堆叠在一起。图像比屏幕大,我希望能够向右滚动,如果我看到图像的最后重复三遍。这些视图一直堆叠在彼此之上,而不是彼此相邻。我无法弄清楚我做错了什么。任何帮助将不胜感激。

这是我的代码

func stackThreeImagesHorizontallyToScrollView(imagetoStack :UIImage,scrollView: UIScrollView){
           
    var prevIoUsAnchor = scrollView.contentLayoutGuide.leadingAnchor

    for i in 0 ..< 3 {
        let imageViewToStack = UIImageView(image: imagetoStack)
        
        switch i {
                    case 0:
                        imageViewToStack.backgroundColor = UIColor.green
                        break
                    case 1:
                        imageViewToStack.backgroundColor = UIColor.purple
                        break
                    case 2:
                        imageViewToStack.backgroundColor = UIColor.yellow
                        break

                    default: break

                    }
        
        scrollView.addSubview(imageViewToStack)

        NSLayoutConstraint.activate([
            imageViewToStack.leadingAnchor.constraint(equalTo: prevIoUsAnchor,constant: 0),imageViewToStack.heightAnchor.constraint(equalToConstant: imagetoStack.size.height),imageViewToStack.widthAnchor.constraint(equalToConstant: imagetoStack.size.width),imageViewToStack.topAnchor.constraint(equalTo: scrollView.topAnchor,imageViewToStack.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor,imageViewToStack.trailingAnchor.constraint(equalTo: imageViewToStack.leadingAnchor,constant: imagetoStack.size.width)
        ])
        
        prevIoUsAnchor = imageViewToStack.trailingAnchor
    }
    
    prevIoUsAnchor.constraint(equalTo: scrollView.trailingAnchor,constant: 0).isActive = true
    
}

解决方法

您忘记将 translatesAutoresizingMaskIntoConstraintsimageViewToStack 属性设置为 false

func stackThreeImagesHorizontallyToScrollView(imageToStack: UIImage,scrollView: UIScrollView) {

        var previousAnchor = scrollView.contentLayoutGuide.leadingAnchor

        for i in 0 ..< 3 {
            let imageViewToStack = UIImageView(image: imageToStack)

            /// ADD THIS LINE
            imageViewToStack.translatesAutoresizingMaskIntoConstraints = false

            switch i {
            case 0:
                imageViewToStack.backgroundColor = UIColor.green
                break
            case 1:
                imageViewToStack.backgroundColor = UIColor.purple
                break
            case 2:
                imageViewToStack.backgroundColor = UIColor.yellow
                break

            default: break

            }

            scrollView.addSubview(imageViewToStack)

            NSLayoutConstraint.activate([
                imageViewToStack.leadingAnchor.constraint(equalTo: previousAnchor,constant: 0),imageViewToStack.heightAnchor.constraint(equalToConstant: imageToStack.size.height),imageViewToStack.widthAnchor.constraint(equalToConstant: imageToStack.size.width),imageViewToStack.topAnchor.constraint(equalTo: scrollView.topAnchor,imageViewToStack.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor,imageViewToStack.trailingAnchor.constraint(equalTo: imageViewToStack.leadingAnchor,constant: imageToStack.size.width)
            ])

            previousAnchor = imageViewToStack.trailingAnchor
        }

        previousAnchor.constraint(equalTo: scrollView.trailingAnchor,constant: 0).isActive = true

    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。