ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Mobile Application Development 2
    COMP4126
    Progress0 / 38 topics
    Topics
    1. Creating an iOS App: Understanding Xcode2. Using the Xcode interface builder and objects library3. Understanding view hierarchy and creating a custom app icon4. Outlets, Actions, and Views: Understanding outlets and actions5. Using text fields, buttons, labels, web views, and page controllers6. Using views with subviews and creating views using code7. Using View Controllers: Working with the single view template8. Exploring the app delegate and adding new view controllers9. Transitioning between multiple view controllers using animations10. Application Templates: Tabbar and master detail templates11. The iOS Keyboard: Customizing for different inputs12. Adjusting text field behaviors and dismissing the keyboard13. Detecting keyboard activities with notification center14. Using scroll view and responding to keyboard activities programmatically15. Working with Different iOS Devices (iPhone & iPad): Detecting device hardware16. Dynamically adjusting graphical layouts and creating universal apps17. Using Table Views: Understanding UITableView and UITableViewCell18. Working with UITableView data source and delegate19. Master detail template, drill-down menus, and navigation20. Using property lists for data persistence and creating multi-section tables21. Supporting Screen Rotations: Portrait and landscape modes22. Handling device rotation and forcing specific orientation23. Dynamically adjusting layouts based on rotation24. Working with Databases: Importing sqlite3 and creating a database25. Writing tables, inserting records, and bundling a database with your app26. Checking for database existence and reading/displaying data27. Using Animations & Video: NSTimer class and object transformations28. Rotation, scaling, translation, animating image arrays, and playing video29. Accessing Integrated iOS Apps: Email, Safari, and SMS30. Working with camera and photo library31. Using Web Services: Consuming and parsing XML and JSON32. Integrating Twitter and Facebook with iOS apps33. Working with iOS Maps and Location Services: MapKit and MKMapView34. Getting and displaying user location and directional information35. Displaying map annotations, disclosure buttons, and reverse geocoding36. Working with iCloud37. Working with the Accelerometer: Gyroscope and accelerometer38. Outputting sensor data and using the Shake API
    COMP4126›Using scroll view and responding to keyboard activities programmatically
    Mobile Application Development 2Topic 14 of 38

    Using scroll view and responding to keyboard activities programmatically

    4 minread
    595words
    Beginnerlevel

    📱 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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count595
      Code examples0
      DifficultyBeginner