ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP4126Scroll View Responding…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP4126

    Mobile Application Development 2

    Progress0 / 38 done
    Topics38
    1Understanding Xcode2Using the Xcode interface builder and objects library3Understanding view hierarchy and creating a custom app icon
    4Understanding outlets and actions5Using text fields, buttons, labels, web views, and page controllers6Using views with subviews and creating views using code
    7Working with the single view template8Exploring the app delegate and adding new view controllers9Transitioning between multiple view controllers using animations
    10Tabbar and master detail templates
    11Customizing for different inputs12Adjusting text field behaviors and dismissing the keyboard13Detecting keyboard activities with notification center14Using scroll view and responding to keyboard activities programmatically
    15Detecting device hardware16Dynamically adjusting graphical layouts and creating universal apps
    17Understanding UITableView and UITableViewCell18Working with UITableView data source and delegate19Master detail template, drill-down menus, and navigation20Using property lists for data persistence and creating multi-section tables
    21Portrait and landscape modes22Handling device rotation and forcing specific orientation23Dynamically adjusting layouts based on rotation
    24Importing sqlite3 and creating a database25Writing tables, inserting records, and bundling a database with your app26Checking for database existence and reading/displaying data
    27NSTimer class and object transformations28Rotation, scaling, translation, animating image arrays, and playing video
    29Email, Safari, and SMS30Working with camera and photo library
    31Consuming and parsing XML and JSON32Integrating Twitter and Facebook with iOS apps
    33MapKit and MKMapView34Getting and displaying user location and directional information35Displaying map annotations, disclosure buttons, and reverse geocoding36Working with iCloud
    37Gyroscope and accelerometer38Outputting sensor data and using the Shake API
    4 min read
    Topic 14 of 38
    Topic Study Note Illustration
    14

    Using scroll view and responding to keyboard activities programmatically

    A Scroll View is a container view that allows users to scroll content that is larger than the screen size (vertically or horizontally).

    πŸ“± Using Scroll View & Responding to Keyboard Activities Programmatically (iOS – Xcode)


    βœ… 1. Definition

    πŸ”Ή UIScrollView

    A Scroll View is a container view that allows users to scroll content that is larger than the screen size (vertically or horizontally).

    πŸ”Ή Keyboard Handling

    It refers to adjusting the UI when the keyboard appears or disappears, so that input fields are not hidden.

    πŸ‘‰ When combined: Scroll View + Keyboard handling = smooth form-based apps (login, registration, chat apps)


    🧠 2. Key Concepts

    πŸ”Ή Why Scroll View is Needed

    • Keyboard often covers text fields
    • Small screens cannot show all content
    • Scroll view allows movement of content

    πŸ”Ή Content Inset

    • Extra space added inside scroll view
    • Used to move content above keyboard

    πŸ”Ή Notification Center

    • Detects keyboard show/hide events

    πŸ—οΈ 3. Basic Structure

    View Controller
       ↓
    Scroll View
       ↓
    Content View
       ↓
    UI Elements (TextFields, Buttons)
    

    βš™οΈ 4. Step-by-Step Setup (Storyboard)


    πŸ”Ή Step 1: Add Scroll View

    • Drag UIScrollView into View Controller

    πŸ”Ή Step 2: Add Content View

    • Inside scroll view, add a UIView
    • This holds all UI elements

    πŸ”Ή Step 3: Add UI Elements

    • Text fields
    • Buttons
    • Labels

    πŸ”Ή Step 4: Add Constraints

    • Scroll View β†’ edges to superview
    • Content View β†’ pinned inside scroll view

    ⌨️ 5. Handling Keyboard Programmatically


    πŸ”Ή Step 1: Add Observers

    override func viewDidLoad() {
        super.viewDidLoad()
        
        NotificationCenter.default.addObserver(self,
            selector: #selector(keyboardWillShow),
            name: UIResponder.keyboardWillShowNotification,
            object: nil)
        
        NotificationCenter.default.addObserver(self,
            selector: #selector(keyboardWillHide),
            name: UIResponder.keyboardWillHideNotification,
            object: nil)
    }
    

    πŸ”Ή Step 2: Add Scroll View Outlet

    @IBOutlet weak var scrollView: UIScrollView!
    

    πŸ”Ή Step 3: Adjust When Keyboard Appears

    @objc func keyboardWillShow(notification: Notification) {
        
        if let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
            
            let keyboardHeight = keyboardFrame.height
            
            scrollView.contentInset.bottom = keyboardHeight
            scrollView.verticalScrollIndicatorInsets.bottom = keyboardHeight
        }
    }
    

    πŸ”Ή Step 4: Reset When Keyboard Hides

    @objc func keyboardWillHide(notification: Notification) {
        scrollView.contentInset.bottom = 0
        scrollView.verticalScrollIndicatorInsets.bottom = 0
    }
    

    πŸ”Ή Step 5: Remove Observers

    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    

    πŸ“Š 6. Diagram Description (for Exams)

    Draw:

    Keyboard Appears
          ↓
    Scroll View Insets Increase
          ↓
    Content Moves Up
          ↓
    TextField Remains Visible
    

    πŸ’‘ 7. Example App

    🎯 Login Form App

    • Username field
    • Password field
    • Login button

    Problem:

    • Keyboard hides password field

    Solution:

    • Scroll view adjusts automatically
    scrollView.contentInset.bottom = keyboardHeight
    

    πŸ“Œ 8. Important Rules / Tips

    • Always embed content inside Scroll View β†’ Content View
    • Use contentInset for keyboard adjustment
    • Remove observers to avoid memory leaks
    • Test on different screen sizes
    • Combine with Auto Layout for best results

    ⚠️ 9. Common Mistakes

    • ❌ Not using Content View inside Scroll View
    • ❌ Forgetting constraints β†’ layout breaks
    • ❌ Not resetting inset on keyboard hide
    • ❌ Hardcoding keyboard height
    • ❌ Missing NotificationCenter observers

    🧠 10. Best Practices

    • Use Auto Layout with Scroll View
    • Always adjust insets dynamically
    • Keep forms inside scroll view for usability
    • Animate UI changes for smooth experience
    • Test with different keyboards (numeric, email, etc.)

    πŸ“ 11. Likely Exam Questions

    1. What is a Scroll View in iOS?
    2. Why is Scroll View used in apps?
    3. How do you handle keyboard appearance programmatically?
    4. Explain contentInset in UIScrollView.
    5. Write code to adjust Scroll View when keyboard appears.
    6. What is Notification Center used for in keyboard handling?
    7. Why is Content View important inside Scroll View?
    8. Explain the relationship between keyboard and scroll view.

    πŸ“š 12. Quick Revision Summary

    • Scroll View = allows scrolling of large content

    • Used in forms and login screens

    • Keyboard handling prevents UI hiding

    • Steps:

      1. Detect keyboard (NotificationCenter)
      2. Adjust scrollView.contentInset
      3. Reset on hide
    • Improves usability and user experience


    ← Previous topic 13
    Detecting keyboard activities with notification center
    Next topic 15 β†’
    Working with Different iOS Devices (iPhone & iPad): Detecting device hardware

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    595words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Mobile Application Development 2