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›Handling device rotation and forcing specific orientation
    Mobile Application Development 2Topic 22 of 38

    Handling device rotation and forcing specific orientation

    4 minread
    603words
    Beginnerlevel

    📱 Handling Device Rotation & Forcing Specific Orientation (iOS – Xcode)


    ✅ 1. Definition

    🔹 Device Rotation Handling

    It means managing how an iOS app responds when the device changes orientation (Portrait ↔ Landscape) so that the UI remains usable and properly arranged.


    🔹 Forcing Specific Orientation

    It means restricting an app so it only works in selected orientations, such as:

    • Portrait only
    • Landscape only
    • Both (restricted per screen)

    🧠 2. Key Concepts

    🔹 Orientation Types

    • 📱 Portrait (vertical)
    • 📱 Landscape Left
    • 📱 Landscape Right

    🔹 UIViewController Control

    Each view controller can define:

    • Allowed orientations
    • Preferred orientation behavior

    🔹 Auto Layout Importance

    • Ensures UI adapts automatically during rotation
    • Prevents layout breaking

    🔄 3. How Rotation Works in iOS

    Device Rotates
          ↓
    iOS detects orientation change
          ↓
    View Controller updates layout
          ↓
    Auto Layout rearranges UI
    

    ⚙️ 4. Handling Device Rotation


    🔹 1. Detect Rotation Change

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        
        if size.width > size.height {
            print("Landscape Mode")
        } else {
            print("Portrait Mode")
        }
    }
    

    🔹 2. Using Trait Collection (Modern Method)

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        
        if traitCollection.verticalSizeClass == .compact {
            print("Landscape detected")
        }
    }
    

    🔹 3. Adjust UI on Rotation

    if UIDevice.current.orientation.isLandscape {
        label.font = UIFont.systemFont(ofSize: 24)
    } else {
        label.font = UIFont.systemFont(ofSize: 18)
    }
    

    🚫 5. Forcing Specific Orientation


    🔹 Method 1: Project Settings (Most Common)

    • Go to Xcode → Target → General

    • Deployment Info

    • Select allowed orientations:

      • ✔ Portrait
      • ✔ Landscape Left/Right

    🔹 Method 2: Restrict in App Delegate

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return .portrait
    }
    

    👉 Forces entire app to portrait mode


    🔹 Method 3: Restrict Per View Controller

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    

    👉 Only this screen stays in portrait


    🔹 Method 4: Force Landscape Only Screen

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscape
    }
    

    📊 6. Diagram Description (for Exams)

    Rotation Handling

    Rotate Device
         ↓
    View Controller detects change
         ↓
    UI rearranges using Auto Layout
    

    Forced Orientation

    App / Screen
         ↓
    Allowed Orientation Rule
         ↓
    Only Portrait OR Only Landscape
    

    💡 7. Example App

    🎯 Video Player App

    • Portrait:

      • Controls + video stacked
    • Landscape:

      • Full-screen video mode

    👉 Often forces landscape for better viewing


    📌 8. Important Rules / Tips

    • Always use Auto Layout for rotation support
    • Prefer per-view-controller orientation control
    • Avoid forcing orientation unless necessary
    • Use traitCollection for modern UI updates
    • Test both orientations in simulator

    ⚠️ 9. Common Mistakes

    • ❌ Forcing orientation for entire app unnecessarily
    • ❌ Ignoring Auto Layout → broken UI
    • ❌ Not handling rotation changes
    • ❌ Using deprecated orientation APIs
    • ❌ Not testing landscape mode

    🧠 10. Best Practices

    • Use adaptive UI instead of fixed orientation locking
    • Force orientation only for special screens (e.g., video player, games)
    • Use Auto Layout + Stack Views
    • Prefer traitCollectionDidChange for modern apps
    • Keep UI flexible across rotations

    📝 11. Likely Exam Questions

    1. What is device rotation in iOS?
    2. How do you detect orientation changes?
    3. Explain forcing orientation in iOS.
    4. How do you restrict an app to portrait mode only?
    5. What is supportedInterfaceOrientations used for?
    6. Differentiate between rotation handling and forced orientation.
    7. Why is Auto Layout important in rotation?
    8. Write code to force landscape mode.

    📚 12. Quick Revision Summary

    • Rotation = switching between portrait & landscape

    • iOS handles rotation automatically using Auto Layout

    • Methods:

      • viewWillTransition
      • traitCollectionDidChange
    • Orientation can be forced using:

      • App settings
      • AppDelegate
      • ViewController override
    • Best practice: use adaptive UI, not fixed orientation


    Previous topic 21
    Supporting Screen Rotations: Portrait and landscape modes
    Next topic 23
    Dynamically adjusting layouts based on rotation

    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 count603
      Code examples0
      DifficultyBeginner