ios - 使用可视格式语言以编程方式将 subview 添加到 ScrollView

标签 ios swift scrollview nslayoutconstraint layoutsubviews

ScrollView - 以编程方式布局 - swift 3

亲爱的 friend 们:我希望有人能在我的大脑被烧毁之前修改这个项目并帮助我。提前致谢。

任务:水平滚动 - 布置一个包含 4 个图像的阵列,240 x 240 的正方形和 20 的间距。 ScrollView 的约束直接在 Storyboard中设置,但图像 subview 是使用可视化格式语言以编程方式添加的。滚动的内容大小假设由这个约束完成。

我所做的:设置图像数组,以编程方式创建 de ImageView 并使用 for in 循环添加数组。使用视觉格式创建约束。可以在本文中找到执行此操作的方法:http://www.apeth.com/iOSBook/ch20.html .

这里是 GitHub 中项目的链接 https://github.com/ricardovaldes/soloScrollEjercicio

Constraints for the ScrollView added directly in the storyboard.

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var myScroll: UIScrollView!

    var carsArray = [UIImage]()
    var constraints = [NSLayoutConstraint]()

    override func viewDidLoad() {
        super.viewDidLoad()

        carsArray = [#imageLiteral(resourceName: "fasto1"), #imageLiteral(resourceName: "fasto2"), #imageLiteral(resourceName: "fasto3"), #imageLiteral(resourceName: "fasto4")]

        var const = [NSLayoutConstraint]()
        var views: [String: UIView]
        var previous: UIImageView? = nil

        for index in 0..<carsArray.count{
            let newImageView = UIImageView()
            newImageView.image  = carsArray[index]
            newImageView.translatesAutoresizingMaskIntoConstraints = false
            myScroll.addSubview(newImageView)
            self.myScroll.setNeedsLayout()


            views = ["newImageView": newImageView, "myScroll": myScroll]

            if previous == nil{
                const.append(contentsOf: NSLayoutConstraint.constraints(withVisualFormat: "H:|[newImageView(240)]",  metrics: nil, views: views))
            }
            else{
                const.append(contentsOf: NSLayoutConstraint.constraints(withVisualFormat: "H:[previous]-20-[newImageView(240)]", metrics: nil, views: ["newImageView": newImageView, "previous": previous!]))
            }

            previous = newImageView

            const.append(contentsOf: NSLayoutConstraint.constraints(withVisualFormat: "H:[previous]|", metrics: nil, views: ["previous": newImageView]))

            const.append(contentsOf: NSLayoutConstraint.constraints(withVisualFormat: "V:|[newImageView(240)]|", metrics: nil, views: views))

        }
        NSLayoutConstraint.activate(const)
    }
}

尽管我尝试了很多组合,但我还是遇到了同样的错误:

2018-04-29 21:24:34.347466-0500 soloScrollEjercicio[12002:1665919] [LayoutConstraints] Unable to simultaneously satisfy constraints.

最佳答案

每一轮循环,你添加一个右侧引脚约束来为每个新的 ScrollView 添加了imageView,但是彼此之间有20个点。

 |-previous-20-newOne-|                                ** second round loop **
                     -20-newOne'-|                     ** third round loop  **

这打破了 imageView 宽度(240)的约束

一种处理方法: 仅将右侧引脚约束添加到最后一个 imageView。

enter image description here

enter image description here

主 Storyboard中的 ScrollView 约束也有中断。

+-------------------+
|          |        |
|-scroll view (240)-|

与 super View 垂直间距的底部不应该在那里。它会 break the scroll view height(240),所以删除它就可以了。

也许你应该试试:

  • 将其约束优先级设置为 999,或其他不等于 1000
  • 取消选中已安装的框
  • 删除它

enter image description here


现在,您的 ScrollView 应该没问题了。

enter image description here


附注我发现你的引用书是基于 iOS 6 的? 2018年,从iOS 10或iOS 11开始可能是更好的选择。

快乐的黑客:)

关于ios - 使用可视格式语言以编程方式将 subview 添加到 ScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50093279/

相关文章:

swift - 更改部分索引背景颜色

ios - 设置 UICollectionView 的 NumberOfItemsInSection

ios - 带有滚动 ViewController 的容器 ViewController

android - view.getDrawingCache() 仅在 Samsung Galaxy note 8.0 中返回 null?

ios - 带有 subview Controller 的分页 ScrollView

ios - 如何在 Xcode 中对图像 Assets 使用本地化?

ios - 应用程序在后台运行3分钟

iOS布局: How to keep a button in the middle an image?

ios - Xcode - 如何在 Storyboard中切换 View

ios - SpriteKit 和 SwiftUI 动画(内存泄漏 - NSXPCConnection?)