Learn Courses My Dashboard

Duplicate buttons in Scroll View issue

In my code, I am seeing one button overlapping another which I need to not overlap. This button “didTapButton” scrolls to it’s last page and the last button “didTapGetStartedBtn” make you go to the next screen-view. The problem is throughout the process I am seeing the buttons overlap in view. How do I stop the overlap?

View Hierarchy:

import UIKit

    class WelcomeViewController: UIViewController {
    
        
    @IBOutlet var pageControl: UIPageControl!
        
    @IBOutlet var holderView: UIView!
    

    
    let scrollView = UIScrollView()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        scrollView.delegate = self
        pageControl.addTarget(self, action: #selector(pageControlDidChange(_:)), for: .valueChanged)
        view.addSubview(pageControl)
        
    }
    
    @objc private func pageControlDidChange(_ sender: UIPageControl) {
        let current = sender.currentPage
        scrollView.setContentOffset(CGPoint(x: CGFloat(current) * view.frame.size.width, y: 0), animated: true)
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        pageControl.frame = CGRect(x: 10, y: view.frame.size.height-100, width: view.frame.size.width-20, height: 70)
        pageControl.transform = CGAffineTransform(scaleX: 2, y: 2)
        
        
        configure()
        
    }
    
    private func configure() {
        // Set up the scrollview
    
        scrollView.frame = holderView.bounds
        holderView.addSubview(scrollView)
        
        let titles = ["", "", "",""]
        
        for x in 0..<4 {
            let pageView = UIView(frame: CGRect(x: CGFloat(x) * holderView.frame.size.width, y: 0, width: holderView.frame.size.width, height: holderView.frame.size.height))
            scrollView.addSubview(pageView)
            
            

            // Title, Image & Button
            let label = UILabel(frame: CGRect(x: 10, y: 10, width: pageView.frame.size.width-20, height: 120))
            let imageView = UIImageView(frame: CGRect(x: 10, y: 10+20+10, width: pageView.frame.size.width-20, height: pageView.frame.size.height - 20 - 0 - 20))
            let button = UIButton(frame: CGRect(x: 10, y: pageView.frame.size.height-60, width: pageView.frame.size.width-20, height: 50))
            let button2 = UIButton(frame: CGRect(x: 10, y: pageView.frame.size.height-60, width: pageView.frame.size.width-20, height: 50))
                
                label.textAlignment = .center
                label.font = UIFont(name: "Helvetica-Bold", size: 32)
                pageView.addSubview(label)
                label.text = titles[x]
                                                      
                imageView.contentMode = .scaleAspectFit
                imageView.image = UIImage(named: "welcome_\(x+1)")
                pageView.addSubview(imageView)
                
                button.layer.cornerRadius = 25
                button.setTitleColor(.white, for: .normal)
                button.backgroundColor = .systemBlue
                button.setTitle("Continue", for: .normal)
            
                if x == 3 {
                    
                    let pageView = UIView(frame: CGRect(x: CGFloat(x) * holderView.frame.size.width, y: 0, width: holderView.frame.size.width, height: holderView.frame.size.height))
                        scrollView.addSubview(pageView)
                        button2.layer.cornerRadius = 25
                        button.setTitle("", for: .normal)

                        button2.setTitle("Get Started", for: .normal)
                    
                        pageView.addSubview(button2)
                    
                    button2.addTarget(self, action: #selector(didTapGetStartedBtn(_:)), for: .touchUpInside)
                    
            }
            
                button.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)
                button.tag = x+1
                pageView.addSubview(button)
                                        
        }
        scrollView.contentSize = CGSize(width: holderView.frame.size.width*4, height: 0)
        scrollView.isPagingEnabled = true
    }
    
    @objc func didTapButton(_ button: UIButton) {
      
        ViewController.Core.shared.setIsNotNewUser()
        
        guard button.tag < 4 else {

            // dismiss
            dismiss(animated: true, completion: nil)
            return
            
        }

        // Scroll to next page
        scrollView.setContentOffset(CGPoint(x: holderView.frame.size.width * CGFloat(button.tag), y: 0), animated: true)

    }
    

    @IBAction func didTapGetStartedBtn(_ button2: UIButton) {
        
        
       guard let vc = storyboard?.instantiateViewController(withIdentifier: "getStarted") as? GetStartedViewController else {
           print("Failed to print vc from storyboard")
           return
            
        }
        present(vc, animated: true)
        
    }
    

}

extension WelcomeViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        pageControl.currentPage = Int(floorf(Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width)))
    }
    
}

I believe I solved the problem. It was as simple as adding a background color.