我有一个 UIScrollView,它是一个仪表板,基本上为用户提供相关信息。它下面有一个寻呼机,目前包含三个 UIView。
我曾经用CGRect作为框架设置不同的视图,将x偏移设置为UIScrollView的宽度*页码。这工作得很好,但是在下面的 UITableView 中滚动时使 UIScrollView 消失很麻烦。
我现在使用带有约束的 AutoLayout 来显示信息,效果很好。但是,我的 UIScrollView 不再滚动。它不会响应我执行的任何滑动手势。但是,它确实响应了寻呼机中的点击,这表明偏移和约束是正确的,我只是无法在其中滚动。看一看:
我的 UIScrollView 是这样组成的:
var dashboardView: UIScrollView = {
let dashboardView = UIScrollView()
dashboardView.translatesAutoresizingMaskIntoConstraints = false
dashboardView.backgroundColor = UIColor.clear
dashboardView.layer.masksToBounds = true
dashboardView.layer.cornerRadius = 10
dashboardView.isPagingEnabled = true
dashboardView.showsHorizontalScrollIndicator = false
dashboardView.showsVerticalScrollIndicator = false
dashboardView.alwaysBounceHorizontal = false
dashboardView.alwaysBounceVertical = false
return dashboardView
}()
然后我像这样设置不同的视图:
for index in 0..<3 {
let currentDash = UIView()
currentDash.backgroundColor = UIColor.lightGray
currentDash.layer.cornerRadius = 10
currentDash.layer.masksToBounds = false
currentDash.translatesAutoresizingMaskIntoConstraints = false
currentDash.isUserInteractionEnabled = false
dashboardView.addSubview(currentDash)
currentDash.topAnchor.constraint(equalTo: dashboardView.topAnchor).isActive = true
currentDash.bottomAnchor.constraint(equalTo: dashboardView.bottomAnchor).isActive = true
currentDash.leadingAnchor.constraint(equalTo: dashboardView.leadingAnchor, constant: dashboardWidth * CGFloat(index)).isActive = true
currentDash.trailingAnchor.constraint(equalTo: dashboardView.leadingAnchor, constant: (dashboardWidth * CGFloat(index)) + dashboardWidth).isActive = true
currentDash.widthAnchor.constraint(equalToConstant: dashboardWidth).isActive = true
currentDash.heightAnchor.constraint(equalToConstant: dashboardHeight).isActive = true
}
当我删除约束(AutoLayout)并使用框架设置它时,它可以完美地工作,如下所示:
dashFrame.origin.x = dashboardWidth * CGFloat(index)
dashFrame.size = CGSize(width: dashboardWidth, height: dashboardHeight)
let currentDash = UIView(frame: dashFrame)
但是,由于设置了框架,视图不会像我那样向上滚动,这就是我想使用 AutoLayout 的原因。
关于我做错了什么的任何想法或建议?我的 ScrollViewDidScroll() 方法没有被调用,因为简单的打印不会在我的控制台中返回任何内容。
我尝试在 currentDash UIView 上将 isUserInteractionEnabled 属性设置为 false,但没有成功。我也试过单独删除所有约束,一些一起删除,等等 - 但这也不起作用 - 我至少需要一个 topAnchor、bottomAnchor、heightAnchor 和 widthAnchor。
哦,当然,我设置了 contentSize 属性:
dashboardView.contentSize = CGSize(width: dashboardWidth * CGFloat(dashboardPager.numberOfPages), height: dashboardHeight)
任何对正确答案的建议或提示将不胜感激。谢谢!
编辑:我还为 UIScrollView 本身设置了约束:
dashboardView.topAnchor.constraint(equalTo: dashboardBG.topAnchor).isActive = true
dashboardView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: normalSpacing).isActive = true
dashboardView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -normalSpacing).isActive = true
dashboardView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
dashboardView.heightAnchor.constraint(equalToConstant: dashboardHeight).isActive = true
dashboardView.delegate = self
问题在这里
currentDash.leadingAnchor.constraint(equalTo: dashboardView.leadingAnchor, constant: dashboardWidth * CGFloat(index)).isActive = true
currentDash.trailingAnchor.constraint(equalTo: dashboardView.leadingAnchor, constant: (dashboardWidth * CGFloat(index)) + dashboardWidth).isActive = true
1-关于视图的前导,第一个视图的前导应该固定在 scrollView 上,第二个视图的前导固定在前一个视图的尾部,依此类推
if index == 0{
// make leading with scrolview leading
else
{
// make leading with prevView trailing
}
因此,使用 scrollView 启动 var 并更改 for 循环的结尾
2- 关于拖尾,只有最后一个视图拖尾被固定到滚动视图的尾随
if index == 2 { // last
// make trailing with scrollView
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句