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

Xcode 以编程方式将视图添加到UIScrollView,xcode,uiscrollview,swift3,autolayout,nslayoutconstraint,Xcode,Uiscrollview,Swift3,Autolayout,Nslayoutconstraint,我已将以下代码添加到UIViewController: override func viewDidLoad() { super.viewDidLoad() viewBrochures.isUserInteractionEnabled = true let brochure1: UIImageView = UIImageView(image: UIImage(named: "image1")!) brochure1.translatesAutoresizingMa

我已将以下代码添加到UIViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    viewBrochures.isUserInteractionEnabled = true
    let brochure1: UIImageView = UIImageView(image: UIImage(named: "image1")!)
    brochure1.translatesAutoresizingMaskIntoConstraints = false
    brochure1.contentMode = .scaleAspectFit
    brochure1.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 400)
    viewBrochures.addSubview(brochure1)
    let brochure2: UIImageView = UIImageView(image: UIImage(named: "image2")!)
    brochure2.translatesAutoresizingMaskIntoConstraints = false
    brochure2.contentMode = .scaleAspectFit
    brochure2.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 400)
    viewBrochures.addSubview(brochure2)
    let brochure3: UIImageView = UIImageView(image: UIImage(named: "image3")!)
    brochure3.translatesAutoresizingMaskIntoConstraints = false
    brochure3.contentMode = .scaleAspectFit
    brochure3.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 400)
    viewBrochures.addSubview(brochure3)
    let brochure4: UIImageView = UIImageView(image: UIImage(named: "image4")!)
    brochure4.translatesAutoresizingMaskIntoConstraints = false
    brochure4.contentMode = .scaleAspectFit
    brochure4.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 400)
    viewBrochures.addSubview(brochure4)


    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure1,
                                                attribute: .trailing,
                                                    relatedBy: .equal,
                                                    toItem: viewBrochures,
                                                    attribute: .trailingMargin,
                                                    multiplier: 1,
                                                    constant: 0))
    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure1,
                                                  attribute: .leading,
                                                  relatedBy: .equal,
                                                  toItem: viewBrochures,
                                                  attribute: .leadingMargin,
                                                  multiplier: 1,
                                                  constant: 0))
    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure1,
                                                  attribute: .top,
                                                  relatedBy: .equal,
                                                  toItem: viewBottomButtons,
                                                  attribute: .bottom,
                                                  multiplier: 1,
                                                  constant: 10))
    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure1,
                                                  attribute: .bottom,
                                                  relatedBy: .equal,
                                                  toItem: brochure2,
                                                  attribute: .top,
                                                  multiplier: 1,
                                                  constant: 10))


    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure2,
                                                  attribute: .trailing,
                                                  relatedBy: .equal,
                                                  toItem: viewBrochures,
                                                  attribute: .trailingMargin,
                                                  multiplier: 1,
                                                  constant: 0))
    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure2,
                                                  attribute: .leading,
                                                  relatedBy: .equal,
                                                  toItem: viewBrochures,
                                                  attribute: .leadingMargin,
                                                  multiplier: 1,
                                                  constant: 0))
    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure2,
                                                  attribute: .top,
                                                  relatedBy: .equal,
                                                  toItem: brochure1,
                                                  attribute: .bottom,
                                                  multiplier: 1,
                                                  constant: 10))
    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure2,
                                                  attribute: .bottom,
                                                  relatedBy: .equal,
                                                  toItem: brochure3,
                                                  attribute: .top,
                                                  multiplier: 1,
                                                  constant: 10))


    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure3,
                                                  attribute: .trailing,
                                                  relatedBy: .equal,
                                                  toItem: viewBrochures,
                                                  attribute: .trailingMargin,
                                                  multiplier: 1,
                                                  constant: 0))
    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure3,
                                                  attribute: .leading,
                                                  relatedBy: .equal,
                                                  toItem: viewBrochures,
                                                  attribute: .leadingMargin,
                                                  multiplier: 1,
                                                  constant: 0))
    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure3,
                                                  attribute: .top,
                                                  relatedBy: .equal,
                                                  toItem: brochure2,
                                                  attribute: .bottom,
                                                  multiplier: 1,
                                                  constant: 10))
    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure3,
                                                  attribute: .bottom,
                                                  relatedBy: .equal,
                                                  toItem: brochure4,
                                                  attribute: .top,
                                                  multiplier: 1,
                                                  constant: 10))


    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure4,
                                                  attribute: .trailing,
                                                  relatedBy: .equal,
                                                  toItem: viewBrochures,
                                                  attribute: .trailingMargin,
                                                  multiplier: 1,
                                                  constant: 0))
    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure4,
                                                  attribute: .leading,
                                                  relatedBy: .equal,
                                                  toItem: viewBrochures,
                                                  attribute: .leadingMargin,
                                                  multiplier: 1,
                                                  constant: 0))
    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure4,
                                                  attribute: .top,
                                                  relatedBy: .equal,
                                                  toItem: brochure3,
                                                  attribute: .bottom,
                                                  multiplier: 1,
                                                  constant: 10))
    viewBrochures.addConstraint(NSLayoutConstraint(item: brochure4,
                                                  attribute: .bottomMargin,
                                                  relatedBy: .greaterThanOrEqual,
                                                  toItem: viewBrochures,
                                                  attribute: .bottom,
                                                  multiplier: 1,
                                                  constant: 10))

}
而且:

override func viewDidLayoutSubviews() {
    scrlMain.contentSize = viewBrochures.bounds.size
}
请注意,我已经在故事板中设置了我的UIScrollView和我的容器视图小册子,我只需要将图像添加到容器中,然后能够垂直滚动它们,因为它们不适合屏幕。在scrollview的容器视图中,我已经有2个其他视图需要在这些新图像的上方,图像需要在2个视图的下方,并且所有这些视图加上2个视图+图像需要在scrollview中一起滚动。这两个视图已经添加到故事板中,没有任何问题。正如您所看到的,我已经创建了这些图像,并以编程方式向它们添加了约束。我为每个图像添加了4个约束,一个用于前导,一个用于尾随,一个用于顶部,一个用于底部。我已将最上面的图像约束到正上方视图的底部,而我已将最下面的图像约束到容器视图的底部,使用更大的或相等的约束。我还看到了在代码中将图像添加到scrollview时,需要将TranslatesAutoResizezingMaskintoConstraints=false添加到图像中,但我不确定它的确切功能以及是否需要


因此,这段代码可以用于添加图像,但问题是scrollview根本不会滚动。我不确定这是否是正确的方法,所以请随时向我建议其他选择。如您所见,我使用了自动布局和布局约束。如果您能看到我的实施过程中是否存在任何问题,请提供任何建议和/或更正。

您的方法有两个主要问题:

使用自动布局时,不应手动设置任何框架。AutoLayout会为您这样做,并且不喜欢您进行干扰。 使用UIScrollView和AutoLayout时,不应设置UIScrollView的contentSize。只要约束设置正确,UIScrollView将自动调整其内容大小并滚动其内容。 此外,您并不真正需要viewBrochures容器视图,除非它除了作为图像的容器之外,还能做任何事情。您只需将上述两个视图和所有图像添加为UIScrollView的直接子视图

要使滚动视图正常工作,您必须执行以下操作:

最顶部的子视图必须具有UIScrollView的顶部约束 所有其他子视图必须具有顶部约束,且子视图的底部约束位于其上方 最底部的子视图必须具有UIScrollView的底部约束 您还必须注意,子视图不会变得更宽,以至于您的scrollview或scrollview也会突然水平滚动。为此,必须在其中一个子视图上设置宽度约束,该约束等于scrollview的宽度

如果你仍然有困难让它工作,看看这一点,我写了一些时间前,我描述了一个类似的情况比你


我还创建了一个工作示例。

Hi。谢谢你的回答。我试过了,我删除了ViewBrokers,并将所有视图直接放在ScrollView下,我确保宽度不超过ScrollView的宽度,但仍然没有运气。。。它仍然无法滚动。您是否已删除所有出现的执行帧=。。。而contentSize=…?@joern必须具有顶部约束和底部约束。但底部约束尚未定义。也没有高度,我无法定义它们,因为我使用的是重构的UIViewController。我的意思是,我昨天花了一整天的时间试图让这些愚蠢的限制生效。按照您的想法,我得到的所有视图都具有相同的高度,整个屏幕相互重叠。除非是Xcode 10测试版,因为这让我发疯。@MicheleDall'Agata请看一下要点:@joern找到了。这是tvOS,很明显,我必须子类UIView来覆盖canBecomeFocused:类myUIView:UIView{override var canBecomeFocused:Bool{return true}}-今天我学到了一些新东西。。谢谢你的要点,非常有帮助!