Ios 在UIScrollView中滚动UIView
这方面有很多问题,但我对约束条件很不了解。所以我添加了一个UIScrollView,我想显示的UIView的高度是700,这是固定的700,没有动态高度。我对UIScrollView的限制是: 对于UIView,约束条件是:Ios 在UIScrollView中滚动UIView,ios,iphone,swift,uiview,uiscrollview,Ios,Iphone,Swift,Uiview,Uiscrollview,这方面有很多问题,但我对约束条件很不了解。所以我添加了一个UIScrollView,我想显示的UIView的高度是700,这是固定的700,没有动态高度。我对UIScrollView的限制是: 对于UIView,约束条件是: 但是它不是滚动的。给滚动视图中的contentView指定高度,而不仅仅是给滚动视图本身指定高度,给滚动视图中的contentView指定高度,而不仅仅是给滚动视图本身指定高度,当我需要滚动视图时,我所做的就是如下所示-只需在故事板中检查您的约束,然后做同样的事情特别注意第
但是它不是滚动的。给滚动视图中的contentView指定高度,而不仅仅是给滚动视图本身指定高度,给滚动视图中的contentView指定高度,而不仅仅是给滚动视图本身指定高度,当我需要滚动视图时,我所做的就是如下所示-只需在故事板中检查您的约束,然后做同样的事情特别注意第二步: 我向层次结构添加了一个scrollView,并使用autolayout对其进行正确布局,例如,如果它应该覆盖viewController的整个视图: 然后,您需要向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),
])
请注意,我将contentView的leftAnchor和rightAnchor约束为self.view,而不是scrollView,以使其具有固定的宽度。但是,顶部和底部锚点被限制在scrollView中,因此当contentView需要更多空间时,它们会被展开并可滚动
现在,您可以向contentView添加所需的所有内容,并使用autolayout对其进行布局,就像contentView是一个具有无限高度的视图一样。或者,您可以根据需要将其高度显式设置为700
当我需要一个可滚动的视图时,我会做如下操作-只需在故事板中检查您的约束,并在那里执行相同的操作,尤其要注意第二步: 我向层次结构添加了一个scrollView,并使用autolayout对其进行正确布局,例如,如果它应该覆盖viewController的整个视图: 然后,您需要向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),
])
请注意,我将contentView的leftAnchor和rightAnchor约束为self.view,而不是scrollView,以使其具有固定的宽度。但是,顶部和底部锚点被限制在scrollView中,因此当contentView需要更多空间时,它们会被展开并可滚动
现在,您可以向contentView添加所需的所有内容,并使用autolayout对其进行布局,就像contentView是一个具有无限高度的视图一样。或者,您可以根据需要将其高度显式设置为700
为scrollView提供自动布局时,请遵循以下方法 像对待任何其他视图对象一样对待ScrollView,并像通常那样应用约束: 在scrollView中获取一个视图,该视图稍后将包含您想要在scrollView中查看的所有视图。像应用于子视图一样应用约束,如下所示: 除了前导、尾随、顶部和底部约束外,还额外指定了宽度和高度。 宽度和高度将定义ScrollView在水平或垂直方向上的滚动量 与直接指定宽度和高度不同,您可能希望指定与此子视图中可能添加的其他内容相关的高度和宽度 提示:如果可以从上到下绘制一条直线,连接子视图的Y轴约束的约束,则不会出现模糊内容错误。宽度也是如此 在编程方面,您可以采用相同的方法:
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提供自动布局时,请遵循以下方法 像对待任何其他视图对象一样对待ScrollView,并像通常那样应用约束: 在scrollView中获取一个视图,该视图稍后将包含您想要在scrollView中查看的所有视图。像应用于子视图一样应用约束,如下所示: 除了前导、尾随、顶部和底部约束外,还额外指定了宽度和高度。 宽度和高度将定义ScrollView在水平或垂直方向上的滚动量 与直接指定宽度和高度不同,您可能希望指定与此子视图中可能添加的其他内容相关的高度和宽度 提示:如果可以从上到下绘制一条直线,连接子视图的Y轴约束的约束,则不会出现模糊内容错误。宽度也是如此 在编程方面,您可以采用相同的方法:
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
nt.删除了scrollview的高度=700,并向内容视图添加了700个高度约束。不起作用。请给出scrollview高度,也就是说300更愿意小于其内容以查看滚动显示。对于scrollview,移除高度=700,并向内容视图添加700高度限制。不起作用。请给出scrollview的高度,也可以说300更喜欢小于其内容以查看滚动显示