如果文本太长,我会建立一个视图,其中显示很多文本,并且需要向下滚动。向下滚动时,我可以看到垂直滚动条在移动,但是视图没有变化。不使用功能scrollViewWillBeginDragging和scrollViewDidScroll(在控制台中什么也没有发生)。我还使用了isScrollEnabled = true,但是它不起作用。我想念什么吗?
import UIKit
//
class TestView : UIScrollView{
var testObject: TestObject? {
didSet {
//set texts
....
//set height for textViews depending on text
refreshHeightOfTextViews()
}
}
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
cv.translatesAutoresizingMaskIntoConstraints = false
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
isScrollEnabled = true
contentSize = CGSize(width: self.frame.width, height: 2000)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var dateFormatter = DateFormatter()
let dateLabel : UILabel = {
let label = UILabel()
label.text = "Date:"
label.font = UIFont(name: (label.font?.fontName)!, size: 20)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let testALabel : UILabel = {
let label = UILabel()
label.text = "TestA:"
label.font = UIFont(name: (label.font?.fontName)!, size: 20)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let testBLabel : UILabel = {
let label = UILabel()
label.text = "TestB:"
label.font = UIFont(name: (label.font?.fontName)!, size: 20)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let testCLabel : UILabel = {
let label = UILabel()
label.text = "TestC:"
label.font = UIFont(name: (label.font?.fontName)!, size: 20)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let testDLabel : UILabel = {
let label = UILabel()
label.text = "TestD:"
label.font = UIFont(name: (label.font?.fontName)!, size: 20)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let contentLabel : UILabel = {
let label = UILabel()
label.text = "Content:"
label.font = UIFont(name: (label.font?.fontName)!, size: 20)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let contentValueTextView : UITextView = {
let textView = UITextView()
textView.text = "A"
textView.textContainerInset = UIEdgeInsetsMake(0, -4, 0, 0)
textView.font = UIFont(name: (textView.font?.fontName)!, size: 20)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.isEditable = false
// textView.backgroundColor = UIColor.red
textView.isScrollEnabled = false
return textView
}()
func setupViews() {
backgroundColor = UIColor.white
addSubview(dateLabel)
addSubview(testALabel)
addSubview(testBLabel)
addSubview(testCLabel)
addSubview(testDLabel)
addSubview(contentLabel)
addSubview(contentValueTextView)
setupLabels()
}
func refreshHeightOfTextViews() {
let contentSize = CGSize(width: self.frame.width, height: .infinity)
let estimatedContentSize = contentValueTextView.sizeThatFits(contentSize)
contentValueTextView.heightAnchor.constraint(equalToConstant: estimatedContentSize.height).isActive = true
}
func setupLabels() {
dateFormatter.dateFormat = "dd.MM.yyyy"
dateLabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
dateLabel.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor).isActive = true
dateLabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
dateLabel.heightAnchor.constraint(equalToConstant: 24).isActive = true
testALabel.leftAnchor.constraint(equalTo: dateLabel.leftAnchor).isActive = true
testALabel.topAnchor.constraint(equalTo: dateLabel.bottomAnchor).isActive = true
testALabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
testALabel.heightAnchor.constraint(equalTo: dateLabel.heightAnchor).isActive = true
testBLabel.leftAnchor.constraint(equalTo: testALabel.leftAnchor).isActive = true
testBLabel.topAnchor.constraint(equalTo: testALabel.bottomAnchor).isActive = true
testBLabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
testBLabel.heightAnchor.constraint(equalTo: dateLabel.heightAnchor).isActive = true
testCLabel.leftAnchor.constraint(equalTo: testBLabel.leftAnchor).isActive = true
testCLabel.topAnchor.constraint(equalTo: testBLabel.bottomAnchor).isActive = true
testCLabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
testCLabel.heightAnchor.constraint(equalTo: dateLabel.heightAnchor).isActive = true
testDLabel.leftAnchor.constraint(equalTo: testCLabel.leftAnchor).isActive = true
testDLabel.topAnchor.constraint(equalTo: testCLabel.bottomAnchor).isActive = true
testDLabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
testDLabel.heightAnchor.constraint(equalTo: dateLabel.heightAnchor).isActive = true
contentLabel.leftAnchor.constraint(equalTo: testDLabel.leftAnchor).isActive = true
contentLabel.topAnchor.constraint(equalTo: testDLabel.bottomAnchor).isActive = true
contentLabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
contentValueTextView.leftAnchor.constraint(equalTo: contentLabel.leftAnchor).isActive = true
contentValueTextView.topAnchor.constraint(equalTo: contentLabel.bottomAnchor).isActive = true
contentValueTextView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("\(scrollView.contentOffset.y)")
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("test")
}
}
class TestController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var testObject: TestObject?
override func viewDidLoad() {
super.viewDidLoad()
let testFrame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
let testView = TestView(frame: testFrame)
testView.testObject = testObject
view.addSubview(testView)
testView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
testView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
testView.widthAnchor.constraint(equalToConstant: view.frame.width).isActive = true
testView.heightAnchor.constraint(equalToConstant: view.frame.height).isActive = true
}
}
答案 0 :(得分:1)
为了使scrollview正确滚动,您需要将所有项目添加到uiView中,并将该uiView添加到scrollView中。新的uiView的高度应设置为大于scrollView的值。除非未明确设置uiScrollView和uiView的高度,否则它也不起作用。您基本上可以明确地告诉您的应用程序,scrollView内部的包含视图大于其自身,因此您应该滚动。
答案 1 :(得分:0)
不应通过添加子视图来像UIScrollViews一样使用UICollectionViews。
集合视图的内容由数据源提供的UICollectionViewCells计算。 您应将内容分为“集合视图”单元格(或将所有内容放到一个视图中,无论如何)。
另一种选择是创建一个补充视图,该视图也应由数据源提供。
请参阅https://developer.apple.com/documentation/uikit/uicollectionviewdatasource