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 PortalCOMP4126Device Rotation Forcin…
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 22 of 38
    Topic Study Note Illustration
    22

    Handling device rotation and forcing specific orientation

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

    📱 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 view and download past papers

    Click on Show Past Papers to load available exam papers.

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

    No headings found

    Study Toolkit

    Subject Portal

    Mobile Application Development 2