IOS Swift4无法滚动滚动视图



我一直在尝试使滚动视图滚动到滚动视图应该显示的程度。但是,我不能。这是我的代码。

func setupMainView() {
// This is where the image view and other UIViews which are supposed to go in the contentview are set up
self.setupImagesView()
self.setupView1()
self.setupView2()
self.setupView3()
self.setupView4()
self.scrollView = UIScrollView()
self.scrollView.backgroundColor = UIColor.white
self.view.addSubview(self.scrollView)
self.automaticallyAdjustsScrollViewInsets = false
self.scrollView.contentInset = UIEdgeInsets.zero
self.scrollView.scrollIndicatorInsets = UIEdgeInsets.zero;
self.contentView = UIView()
self.scrollView.layer.masksToBounds = false
self.scrollView.backgroundColor = UIColor.white
self.scrollView.layer.borderWidth = 0
self.scrollView.layer.borderColor = UIColor.white.cgColor
self.view.addSubview(scrollView)
self.contentView.addSubview(imagesScrollView)
self.contentView.addSubview(view1)
self.contentView.addSubview(view2)
self.contentView.addSubview(view3)
self.contentView.addSubview(view4)
self.scrollView.addSubview(contentView)
self.scrollView.contentInset = UIEdgeInsets.zero
self.scrollView.scrollIndicatorInsets = UIEdgeInsets.zero;
var scrollViewHeight:CGFloat = 0.0;
for _ in self.scrollView.subviews {
scrollViewHeight += view.frame.size.height
}
var newHeight = scrollViewHeight * 1.1 + offset + 100
scrollView.contentSize = CGSize(width:screenWidth, height:newHeight)
scrollView.reloadInputViews()
}

视图正在加载等,但我无法管理滚动。不知何故,它要么太少,要么太多。

现在,我尝试将contentSize的高度设置为scrollViewHeight和它的两倍等。我注意到,它将滚动多少是不可预测的。从1.1更改为1.6。视图下方有太多的白屏幕,将其更改为1.1或1.2,甚至不会滚动到底部。

请注意,所有内容都是以编程方式设置的,没有故事板等。还要注意,我需要支持所有版本>10.

我在这里有点迷路了。我做错了什么?

这是一种非常古老的配置滚动视图的方法,应该使用自动布局。

而且,你做错了很多事情。。。

首先,我们假设您在未显示的代码中设置各种子视图的框架。

但是,显示的代码创建了一个滚动视图并将其添加到self.view中,但您从未设置滚动视图的框架。

此外,这部分代码:

for _ in self.scrollView.subviews {
scrollViewHeight += view.frame.size.height
}

您添加了几个视图作为contentView的子视图,然后添加了contentView作为scrollView的唯一子视图。

而且您正试图将scrollViewHeight增加根视图的高度,而不是scrollView子视图的高度。

因此,scrollViewHeight将仅仅是self.view的高度。

你可能想做的是求和contentView.subviews:的高度

var contentViewHeight: CGFloat = 0

for v in contentView.subviews {
contentViewHeight += v.frame.height
}
contentView.frame.size.height = contentViewHeight

然后将滚动视图的contentSize.height设置为contentView框架的高度。

这里有一个非常非常基本的示例,使用显式设置的帧大小——不过,您应该再次开始使用自动布局:

class SimpleScrollViewController: UIViewController {
var imagesScrollView: UIView!
var view1: UIView!
var view2: UIView!
var view3: UIView!
var view4: UIView!
var contentView: UIView!
var scrollView: UIScrollView!

override func viewDidLoad() {
super.viewDidLoad()
setupMainView()
}
func setupMainView() {
// This is where the image view and other UIViews which are supposed to go in the contentview are set up
self.setupImagesView()
self.setupView1()
self.setupView2()
self.setupView3()
self.setupView4()

self.scrollView = UIScrollView()

// let's use a color other than white so we can see the frame of the scrollView
self.scrollView.backgroundColor = UIColor.cyan

self.view.addSubview(self.scrollView)
self.automaticallyAdjustsScrollViewInsets = false
self.scrollView.contentInset = UIEdgeInsets.zero
self.scrollView.scrollIndicatorInsets = UIEdgeInsets.zero;

self.contentView = UIView()
self.contentView.backgroundColor = UIColor.lightGray

self.scrollView.layer.masksToBounds = false
self.scrollView.layer.borderWidth = 0
self.scrollView.layer.borderColor = UIColor.white.cgColor
self.view.addSubview(scrollView)

self.contentView.addSubview(imagesScrollView)
self.contentView.addSubview(view1)
self.contentView.addSubview(view2)
self.contentView.addSubview(view3)
self.contentView.addSubview(view4)
self.scrollView.addSubview(contentView)
self.scrollView.contentInset = UIEdgeInsets.zero
self.scrollView.scrollIndicatorInsets = UIEdgeInsets.zero;

var contentViewHeight: CGFloat = 0

for v in contentView.subviews {
contentViewHeight += v.frame.height
}
contentView.frame.size.height = contentViewHeight

// don't know what you're doing here....
//scrollView.reloadInputViews()
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

// here is where you know the frame of self.view
//  so, make the scroll view cover the entire view
scrollView.frame = view.frame

// now, make contentView width equal to scrollView width
contentView.frame.size.width = scrollView.frame.size.width

// set the scrollView's content size
scrollView.contentSize = contentView.frame.size
}

func setupImagesView() -> Void {
imagesScrollView = UIView()
imagesScrollView.backgroundColor = .red
imagesScrollView.frame =  CGRect(0, 0, 300, 100)
}
func setupView1() -> Void {
view1 = UIView()
view1.backgroundColor = .green
view1.frame = CGRect(20, imagesScrollView.frame.maxY, 300, 200)
}
func setupView2() -> Void {
view2 = UIView()
view2.backgroundColor = .blue
view2.frame = CGRect(40, view1.frame.maxY, 300, 250)
}
func setupView3() -> Void {
view3 = UIView()
view3.backgroundColor = .yellow
view3.frame = CGRect(60, view2.frame.maxY, 200, 275)
}
func setupView4() -> Void {
view4 = UIView()
view4.backgroundColor = .orange
view4.frame = CGRect(80, view3.frame.maxY, 200, 100)
}
}

如果我没记错的话,为了让滚动视图工作,你需要实现几个委托方法。您还需要几个属性集。

contentSize是一我认为最小和最大尺寸

请参阅:https://developer.apple.com/documentation/uikit/uiscrollviewdelegate

另请参阅斯坦福大学的Paul Hagarty开发IOS 11应用程序,快速播放第9集,获取UIScrollView 上的大量信息

https://www.youtube.com/watch?v=B281mrPUGjg

查找到滚动视图信息的大约31分钟。

这可能有助于以程序方式设置UIScrollView

https://sheikhamais.medium.com/how-to-use-the-new-uiscrollview-programmatically-baf270ee9b4

最新更新