ScrollView не работает IOS
Мой ScrollView содержит представление контейнера, которое, в свою очередь, имеет textView, высота которого зависит от содержимого и является динамической. Я включил прокрутку для просмотра текста, но, похоже, она не работает. Я пытался установить размер содержимого представления прокрутки даже в viewDidLayouts также, но это также не работает. Код как ниже:
import UIKit
class DiaryViewViewController: UIViewController, UITextViewDelegate, UIScrollViewDelegate, TagListViewDelegate {
var scrollView: UIScrollView!
var containerView = UIView()
let diaryDate = UITextView()
var tagListView: TagListView!
var assets: [DKAsset]?
var diaryEntryText = UITextView()
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView = UIScrollView()
self.scrollView.delegate = self
containerView = UIView()
self.scrollView.delegate = self
scrollView.isDirectionalLockEnabled = true
containerView.translatesAutoresizingMaskIntoConstraints = false
//to fetch height of screen
let screenSize = UIScreen.main.bounds
let width = screenSize.width
let height = screenSize.height
//Top Bar
let topBar = UIView(frame:CGRect(x: 0,y: 0, width: width, height: 60))
topBar.backgroundColor = UIColor.white
topBar.layer.shadowColor = UIColor.gray.cgColor
topBar.layer.shadowOffset = CGSize(width: 0, height: 3)
topBar.layer.shadowOpacity = 1
topBar.layer.masksToBounds = false
topBar.layer.shadowRadius = 8.0;
topBar.translatesAutoresizingMaskIntoConstraints = false
//Label - Title
let titleLabel = UILabel(frame:CGRect(x: width * 0.3, y: 13, width: width * 0.55, height: 40))
titleLabel.text = "23 December 2018"
titleLabel.contentMode = UIViewContentMode.center
//include all in view
topBar.addSubview(titleLabel)
containerView.addSubview(topBar)
let contentSize = diaryEntryText.sizeThatFits(diaryEntryText.bounds.size)
diaryEntryText.text = "------Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiudaLorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. erunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiudaLorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua., uda.-----------------"
diaryEntryText.frame.size.width = self.view.frame.width - 40
diaryEntryText.frame.size.height = contentSize.height
diaryEntryText.frame = CGRect(x:20, y:80, width:self.view.frame.width - 2, height:contentSize.height)
diaryEntryText.isScrollEnabled = true
diaryEntryText.textAlignment = .justified
containerView.addSubview(diaryEntryText)
let fixedWidthText = self.view.frame.width - 40
diaryEntryText.sizeThatFits(CGSize(width: fixedWidthText, height: CGFloat.greatestFiniteMagnitude))
let newSizeText = diaryEntryText.sizeThatFits(CGSize(width: fixedWidthText, height: CGFloat.greatestFiniteMagnitude))
var newFrameText = diaryEntryText.frame
newFrameText.size = CGSize(width: max(newSizeText.width, fixedWidthText), height: newSizeText.height)
diaryEntryText.frame = newFrameText
self.tagListView = TagListView()
tagListView.delegate = self
let contentSizeTag = tagListView.frame.size.height
let yTag = newSizeText.height + 10 + 80
var tagFrame = self.tagListView.frame
tagFrame.size.width = self.view.frame.width-2
tagFrame.size.height = contentSizeTag
tagFrame.origin.x = 20
tagFrame.origin.y = yTag
tagListView.frame = tagFrame
let tagArrayList = ["happy","enjoy","try"]
for element in tagArrayList {
tagListView.addTag(element)
}
containerView.addSubview(tagListView)
let fixedWidthTag = self.view.frame.width - 40
tagListView.sizeThatFits(CGSize(width: fixedWidthTag, height: CGFloat.greatestFiniteMagnitude))
let newSizeTag = diaryEntryText.sizeThatFits(CGSize(width: fixedWidthTag, height: CGFloat.greatestFiniteMagnitude))
var newFrameTag = tagListView.frame
newFrameTag.size = CGSize(width: max(newSizeTag.width, fixedWidthTag), height: newSizeTag.height)
tagListView.frame = newFrameTag
let contentSizeDate = diaryDate.frame.size.height
let yDate = yTag + 20
diaryDate.text = "HSR Layout, Bangalore"
diaryDate.frame = CGRect(x: 20, y: yDate, width: self.view.frame.width - 2, height:contentSizeTag+20)
diaryDate.textColor = UIColor.cyan
containerView.addSubview(diaryDate)
scrollView.addSubview(containerView)
self.view.addSubview(scrollView)
if #available(iOS 11.0, *) {
let top = NSLayoutConstraint.init(item: containerView, attribute: .top, relatedBy: .equal, toItem: self.view.safeAreaLayoutGuide, attribute: .top, multiplier: 1.0, constant: 0.0)
let leading = NSLayoutConstraint.init(item: containerView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 0.0)
let trailing = NSLayoutConstraint.init(item: containerView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1.0, constant: 0.0)
let height = NSLayoutConstraint.init(item: containerView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 60.0)
NSLayoutConstraint.activate([top, leading, trailing, height])
} else {
// Fallback on earlier versions
}
self.scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: yDate + contentSizeDate)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.frame = view.bounds
containerView.frame = CGRect(x:0, y:0, width:scrollView.contentSize.width, height:scrollView.contentSize.height)
/* I tried using this, but doesn't seem to work
let newSizeText = diaryEntryText.sizeThatFits(CGSize(width: self.view.frame.width - 40, height: CGFloat.greatestFiniteMagnitude))
let yTag = newSizeText.height + 10 + 80
let yDate = yTag + 20
let contentSizeDate = diaryDate.frame.size.height
self.scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: yDate + contentSizeDate)
*/
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
1 ответ
Вам не хватает следующих свойств:
Swift 4.0
diaryDate.showsVerticalScrollIndicator = true
diaryDate.isScrollEnabled = true
diaryDate.scrollRangeToVisible(NSMakeRange(0, 0))
diaryDate.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
Вам также может понадобиться установить UserInteractionEnabled
как ДА.