problem: when I load my application with data, then if the PageView is larger than the text size, then it is simply cut off, there is no way to scroll down. And if the content fits on the screen, then the button is not clickable.
what i do: I need to make a UIPageControll toggle between View containing: text, picture, text, button. The button toggles to or ends the next UIPageControll. I create it using the code below:
class PageControllVC: UIViewController {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!
var arrayQuestion = [Question]()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(true, animated: true)
pageControl.numberOfPages = arrayQuestion.count
pageControl.addTarget(self, action: #selector(pageControlDidChange(_:)), for: .valueChanged)
scrollView.isPagingEnabled = true
scrollView.delegate = self
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if scrollView.subviews.count == 2 {
configureScrollView()
}
}
private func configureScrollView() {
for i in 0..<arrayQuestion.count {
var frame = CGRect()
frame.origin.y = 0
frame.origin.x = scrollView.frame.size.width * CGFloat(i)
frame.size = scrollView.frame.size
let view = PageView(frame: frame)
view.delegate = self
view.setContent(question: arrayQuestion[i])
scrollView.addSubview(view)
}
scrollView.contentSize = CGSize(width: view.frame.size.width * CGFloat(arrayQuestion.count), height: scrollView.frame.size.height)
}
@objc private func pageControlDidChange(_ sender: UIPageControl) {
let current = sender.currentPage
scrollView.setContentOffset(CGPoint(x: CGFloat(current) * view.frame.size.width, y: 0), animated: true)
}
@IBAction func closeButtonPress(_ sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
}
extension PageControllVC: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
pageControl.currentPage = Int(floorf(Float(scrollView.contentOffset.x / scrollView.frame.size.width)))
}
}
extension PageControllVC: PageViewDelegate {
func pressNexButton() {
if pageControl.currentPage == arrayQuestion.count {
self.navigationController?.popViewController(animated: true)
} else {
let current = pageControl.currentPage + 1
scrollView.setContentOffset(CGPoint(x: CGFloat(current) * view.frame.size.width, y: 0), animated: true)
}
}
}
As child views for scrollView, I use my PageView class, which looks like this:

And code:
protocol PageViewDelegate: AnyObject {
func pressNexButton()
}
class PageView: UIView, NibLoadableView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var nextPageButton: UIButton!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var contentScrollView: UIView!
weak var delegate: PageViewDelegate?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
func xibSetup() {
contentView = loadViewFromNib()
contentView.frame = bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(contentView)
}
func setContent(question: Question) {
titleLabel.text = question.titile
titleLabel.sizeToFit()
descriptionLabel.text = question.description
descriptionLabel.sizeToFit()
imageView.image = UIImage(named: question.imageName)
nextPageButton.setTitle(question.titileButton, for: .normal)
}
@IBAction func nextPageButtonPress(_ sender: Any) {
delegate?.pressNexButton()
}
}
So, when I load my application with data, then if the PageView is larger than the text size, then it is simply cut off, there is no way to scroll down. And if the content fits on the screen, then the "Good" button is not clickable to go to the next UIPageControll element. what am I doing wrong?
