I have a UIWebView
that I would like to load different URLs depending on whether its portrait or landscape. How can I figure out what my current orientation is inside viewWillAppear
?
orientation. isLandscape { print("Device is in landscape mode") } else { print("Device is in portrait mode") } } override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { determineMyDeviceOrientation() } override func didReceiveMemoryWarning() { super.
Another way to detect device orientations is with the function traitCollectionDidChange(_:). The system calls this method when the iOS interface environment changes.
Use UIApplication's statusBarOrientation
property. You may run into problems if you use the device orientation if it is UIDeviceOrientationFaceUp
or UIDeviceOrientationFaceDown
.
Example
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation))
{
// Do something when in landscape
}
else
{
// Do something when in portrait
}
UIDeviceOrientation getCurrentOrientation() {
UIDevice *device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation currentOrientation = device.orientation;
[device endGeneratingDeviceOrientationNotifications];
return currentOrientation;
}
It's up to you to convert the UIDeviceOrientation
to UIInterfaceOrientation
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With