How to constrain autorotation to a single orientation for some views, while allowing all orientations on others?

The short answer is that you’re using UINavigationController, and that won’t work like you want it to. From Apple’s docs: Why won’t my UIViewController rotate with the device? All child view controllers in your UITabBarController or UINavigationController do not agree on a common orientation set. To make sure that all your child view controllers rotate … Read more

Detecting if a device is able to change orientation in JavaScript

Detecting mobile devices: Simple browser sniffing if (/mobile/i.test(navigator.userAgent)) {…} jQuery.browser.mobile plug-in (exhaustive browser sniffing) Simple test for touch events if (‘ontouchstart’ in window) {…} Advanced test for touch events: if ((‘ontouchstart’ in window) || // Advanced test for touch events (window.DocumentTouch && document instanceof DocumentTouch) || ((hash[‘touch’] && hash[‘touch’].offsetTop) === 9)) {…} Optionally use onorientationchange … Read more

iOS: Disable Autorotation for a Subview

Autorotation is handled by a view’s UIViewController (shouldAutorotateToInterfaceOrientation:), so one approach is to arrange your hierarchy such that rotatable views are managed by one view controller, and non-rotatable views by another view controller. Both of these UIViewController’s root views then need adding to the window/superview. The subtlety here is that if you have two view … Read more

Android phone orientation overview including compass

You might want to check out the One Screen Turn Deserves Another article. It explains why you need the rotation matrix. In a nutshell, the phone’s sensors always use the same coordinate system, even when the device is rotated. In applications that are not locked to a single orientation, the screen coordinate system changes when … Read more

Flutter: How to set and lock screen orientation on-demand

First import the services package: import ‘package:flutter/services.dart’; This will give you access to the SystemChrome class, which “Controls specific aspects of the operating system’s graphical interface and how it interacts with the application.” When you load the Widget, do something like this: @override void initState(){ super.initState(); SystemChrome.setPreferredOrientations([ DeviceOrientation.landscapeRight, DeviceOrientation.landscapeLeft, ]); } then when I leave … Read more

Detect viewport orientation, if orientation is Portrait display alert message advising user of instructions

if(window.innerHeight > window.innerWidth){ alert(“Please use Landscape!”); } jQuery Mobile has an event that handles the change of this property… if you want to warn if someone rotates later – orientationchange Also, after some googling, check out window.orientation (which is I believe measured in degrees…) EDIT: On mobile devices, if you open a keyboard then the … Read more

How to lock orientation of one view controller to portrait mode only in Swift

Things can get quite messy when you have a complicated view hierarchy, like having multiple navigation controllers and/or tab view controllers. This implementation puts it on the individual view controllers to set when they would like to lock orientations, instead of relying on the App Delegate to find them by iterating through subviews. Swift 3, … Read more