在UIScrollView中滚动UIView

这有很多问题,但我对约束非常不满意。 所以我添加了一个UIScrollView ,我要显示的UIView高度为700 ,这是固定的700没有动态高度。 我对UIScrollView的约束是:

在此处输入图像描述

对于UIView ,约束是:

在此处输入图像描述

但它不是滚动。

我需要一个可滚动的视图时所做的是 – 只需在故事板中重复你的约束并在那里做同样的事情(特别注意第二步):

  1. 我将一个scrollView添加到层次结构中并使用autolayout来正确布局它,例如,如果它应该覆盖viewController的整个view

     scrollView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ scrollView.leftAnchor.constraint(equalTo: self.view.leftAnchor), scrollView.rightAnchor.constraint(equalTo: self.view.rightAnchor), scrollView.topAnchor.constraint(equalTo: self.view.topAnchor), scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor), ]) 
  2. 然后你需要向scrollView添加一个contentView并为它提供一个合适的布局约束,所以如果你想在上面开始的例子中使用垂直滚动的scrollView ,你需要遵循自动布局约束:

     contentView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ // horizontal anchors of contentView are constrained to scrollView superview // to prevent it from scrolling horizontally contentView.leftAnchor.constraint(equalTo: self.view.leftAnchor), contentView.rightAnchor.constraint(equalTo: self.view.rightAnchor), // but vertical anchors of contentView are constrained to // scrollView to allow scrolling contentView.topAnchor.constraint(equalTo: scrollView.topAnchor), contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor), ]) 

    请注意,我将contentViewleftAnchorrightAnchor约束为leftAnchor ,而不是scrollView以使其具有固定宽度。 但是,top和bottom锚点被约束到scrollView,因此当contentView需要更多空间时,它们会被展开和滚动。

  3. 现在,您向contentView添加了所需的所有内容,并使用autolayout进行布局,就像contentView是具有无限高度的视图一样。 或者你可以根据需要明确地将其高度设置为700。

当您将AutoLayout提供给scrollView时,请按照以下方法。

像任何其他视图对象一样处理ScrollView并像往常一样应用约束:

在此处输入图像描述

在scrollView中获取一个视图,该视图稍后将包含您在ScrollView中想要的所有视图。 像应用于子视图一样应用约束,如下所示:

在此处输入图像描述

除了前导,尾随,顶部和底部约束之外,还指定了宽度和高度。 宽度和高度将定义ScrollView在水平或垂直方向上滚动的程度。

您可能希望指定与此子视图中可能添加的其他内容相关的高度和宽度,而不是直接指定宽度和高度。

提示:如果可以从上到下绘制直线,连接子视图的Y轴约束的约束,则不会得到ambiguos内容错误。 宽度也是如此。

以编程方式,您可以遵循相同的方法:

  let scrollView = UIScrollView() self.view.addSubview(scrollView) scrollView.translatesAutoresizingMaskIntoConstraints = false let safeArea = view.safeAreaLayoutGuide scrollView.topAnchor.constraint(equalTo: safeArea.topAnchor, constant: 0).isActive = true scrollView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true scrollView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true scrollView.backgroundColor = .gray let scrollContentView = UIView() scrollView.addSubview(scrollContentView) scrollContentView.translatesAutoresizingMaskIntoConstraints = false scrollContentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true scrollContentView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true scrollContentView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true scrollContentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true scrollContentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true scrollContentView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true scrollContentView.backgroundColor = .green 

您可以根据需要增加scrollContentView的heightAnchor或widthAnchor。

将高度赋予scrollview内的contentView,不仅仅是scrollview本身