How can I create reusable view controllers inside of UIScrollView?

General Tech Bugs & Fixes 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am trying to create a full page sized horizontally scrolling UIScrollView. On each page I am adding instances of the same UIViewController class. I would like to create some kind of reusable functionality to conserve both memory and processor use needed. Below is a basic implementation I have created with some toying around with how reusability might work although Im not quite sure. Thank you for any help you can offer.

Current UIScroll ViewController Model

   let scrollView:UIScrollView = {
   let scrollView = UIScrollView(frame: CGRect.zero)
    scrollView.isPagingEnabled = true
    scrollView.backgroundColor = UIColor.gray
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.showsVerticalScrollIndicator = false
    scrollView.showsHorizontalScrollIndicator = true
    return scrollView
}()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    scrollView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: self.view.bounds.size)
    self.view.addSubview(scrollView)
    scrollView.delegate = self
    scrollView.contentSize = CGSize(width: 3 * self.view.frame.width, height: self.view.frame.height)

    NSLayoutConstraint.activate([
        scrollView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0),
        scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0),
        scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0),
        scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0)
        ])

    let viewController1 = UIViewController()
    viewController1.view.backgroundColor = UIColor.red
    let viewController2 = UIViewController()
    viewController2
                                                
                                                
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.