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 PortalCOMP4126Detecting Keyboard Act…
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
    3 min read
    Topic 13 of 38
    Topic Study Note Illustration
    13

    Detecting keyboard activities with notification center

    The Notification Center in iOS is a system that allows apps to observe (listen to) events happening in the system or app , such as keyboard appearance...

    πŸ“± Detecting Keyboard Activities with Notification Center (iOS – Xcode)


    βœ… 1. Definition

    The Notification Center in iOS is a system that allows apps to observe (listen to) events happening in the system or app, such as keyboard appearance and disappearance.

    πŸ‘‰ In simple terms: It lets your app know when the keyboard shows, hides, or changes size.


    🧠 2. Key Concepts

    πŸ”Ή NotificationCenter

    • A built-in iOS system class
    • Sends and receives event notifications

    πŸ”Ή Keyboard Notifications

    iOS provides predefined notifications for keyboard events:

    Notification Meaning
    keyboardWillShow Keyboard is about to appear
    keyboardDidShow Keyboard has appeared
    keyboardWillHide Keyboard is about to disappear
    keyboardDidHide Keyboard has disappeared
    keyboardWillChangeFrame Keyboard size/position changing

    πŸ”Ή Observer

    • A listener that waits for notifications
    • Your ViewController acts as an observer

    βš™οΈ 3. How Notification Center Works

    Keyboard Event β†’ Notification Center β†’ Observer (ViewController) β†’ Action
    

    πŸ—οΈ 4. Detecting Keyboard Events (Step-by-Step)


    πŸ”Ή 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: Handle Keyboard Appearing

    @objc func keyboardWillShow(notification: Notification) {
        print("Keyboard is shown")
    }
    

    πŸ”Ή Step 3: Handle Keyboard Hiding

    @objc func keyboardWillHide(notification: Notification) {
        print("Keyboard is hidden")
    }
    

    πŸ“ 5. Getting Keyboard Size (Important)

    Sometimes we need keyboard height to adjust UI.

    @objc func keyboardWillShow(notification: Notification) {
        if let userInfo = notification.userInfo,
           let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
            
            let keyboardHeight = keyboardFrame.height
            print("Keyboard height: \(keyboardHeight)")
        }
    }
    

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

    Draw:

    Keyboard Appears
          ↓
    NotificationCenter sends event
          ↓
    ViewController receives notification
          ↓
    Adjust UI / Move TextField
    

    πŸ’‘ 7. Real Use Case Example

    🎯 Chat App

    Problem:

    • Keyboard covers text field

    Solution:

    • Detect keyboard height
    • Move input field upward
    @objc func keyboardWillShow(notification: Notification) {
        if let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
            self.view.frame.origin.y = -keyboardFrame.height / 2
        }
    }
    
    @objc func keyboardWillHide(notification: Notification) {
        self.view.frame.origin.y = 0
    }
    

    🧹 8. Removing Observers (Important Rule)

    Always remove observers to avoid memory issues:

    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    

    πŸ“Œ 9. Important Rules / Tips

    • Always register observers in viewDidLoad()
    • Remove observers in deinit
    • Use keyboard height to adjust UI layout
    • Handle both show and hide events
    • Avoid hardcoding UI shifts

    ⚠️ 10. Common Mistakes

    • ❌ Not removing observers β†’ memory leaks
    • ❌ Ignoring keyboard height
    • ❌ UI not adjusting properly β†’ text fields hidden
    • ❌ Using fixed values instead of dynamic keyboard size

    🧠 11. Best Practices

    • Use Auto Layout instead of manual frame shifting when possible
    • Animate UI changes with keyboard
    • Handle different device sizes
    • Use keyboard notifications for responsive design

    πŸ“ 12. Likely Exam Questions

    1. What is Notification Center in iOS?
    2. How do you detect keyboard appearance?
    3. List keyboard-related notifications.
    4. Write code to detect keyboard show event.
    5. Why is keyboard height important?
    6. Explain how to adjust UI when keyboard appears.
    7. What is the role of observers?
    8. Why should observers be removed?

    πŸ“š 13. Quick Revision Summary

    • Notification Center detects system events

    • Used for keyboard tracking in apps

    • Key notifications:

      • Show
      • Hide
      • Frame change
    • Steps:

      1. Add observer
      2. Handle event
      3. Adjust UI
      4. Remove observer
    • Used in chat apps, forms, login screens


    ← Previous topic 12
    Adjusting text field behaviors and dismissing the keyboard
    Next topic 14 β†’
    Using scroll view and responding to keyboard activities programmatically

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    3mremaining
    573words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Mobile Application Development 2