我想允許用戶向左或向右滾動,但只向前滾動一個屏幕。ScrollView不會在iOS中滾動
//
// MainViewController.swift
// Calendar
//
// Created by Andy on 7/22/17.
// Copyright © 2017 Andy. All rights reserved.
//
import UIKit
class MainViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var ScrollView: UIScrollView!{
didSet{
ScrollView.delegate = self
ScrollView.contentSize = CGSize(width: ScrollView.frame.width * 3, height: ScrollView.frame.height)
}
}
@IBOutlet weak var center: UIView!
@IBOutlet weak var left: UIView!
@IBOutlet weak var right: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ReusableViewController {
if let identifier = segue.identifier{
destination.currentYearOffSet = 0
destination.currentMonthOffSet = 0
switch identifier {
case "center":
destination.position = .center
case "left":
destination.position = .left
case "right":
destination.position = .right
default:
fatalError("Unexpected Segue Identifier")
}
}
}
}
}
這個控制器沒有什麼比得到滾動工作。在scrollview中有三個由其他控制器管理的containerView。但這根本行不通。
我已經做了以下解決問題,但徒勞無功:1.檢查scrollView的內容大小。 2.連接控制器和三個containerViews。 3.設置scrollView委託,但我還沒有實現任何方法(也許這是原因,但我不知道該怎麼實現)
另外,當控制器什麼都不做時,我需要連接這三個containerViews關於他們?
編輯:我刪除了:1. contentSize 2. UIScrollViewDelegate 3.三個containerViews的插座。這確實使我的代碼更好,但scrollView仍然無法滾動。
EDIT2:我添加一個視圖,它正是我想要scrollView.contentSize作爲scrollView的直接子視圖的大小。內容高度似乎已經解決了,因爲它只是說內容寬度不明確。它想爲我添加的視圖添加前後約束,但我希望它只是在scrollView中居中。現在我需要設置前後約束來讓autoLayout知道contentWidth。我如何根據其超視圖的寬度來設置前導和尾隨offSet,或者我應該採取其他措施來解決此問題?
我非常感謝您的回覆。但我真的很抱歉,我不認爲我理解你。你的意思是我不需要設置scrollView委託和contentSize,或者我應該把它們設置在別的地方?而對於水平滾動,你的意思是我可以在故事板內做到這一點?也許我的問題在於故事板文件? –
是的,你可以做你的故事板內沒有做任何代碼 –
是的,你不需要設置scrollView委託和contentSize –