8.2. Creating the Controller

Let's put the following code in the ViewController.swift:

//
//  ViewController.swift
//  App8_scroller
//
//  Created by Sakib Miazi on 5/24/23.
//

import UIKit

class ViewController: UIViewController {

    let homeScreen = ScrollScreenView()
    
    override func loadView() {
        view = homeScreen
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //MARK: hide Keyboard on tapping the screen...
        hideKeyboardOnTapOutside()
    }
    
    //MARK: hide keyboard logic...
    func hideKeyboardOnTapOutside(){
        //MARK: recognizing the taps on the app screen, not the keyboard...
        let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(hideKeyboardOnTap))
        view.addGestureRecognizer(tapRecognizer)
    }
    
    @objc func hideKeyboardOnTap(){
        //MARK: removing the keyboard from screen...
        view.endEditing(true)
    }
}

Here, we are loading the view on loadView() method; and also including the methods to hide the iOS keyboard by tapping outside (Hiding Keyboard when tapped outside).

Let's run the app now.

Now that you know how to use scrollable views. Use it carefully when you use it.

Last updated

Was this helpful?