UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How can I prevent interface orientation for a specific view?

Forums > SwiftUI

The title says it all. I know how to use plist to switch off device orientation for the app but i want to do it at the view level.

1      

Part of the learning adventure for us is seeing what you tried and failed. It would be great if you shared some of your knowledge and code, letting us learn from your mistakes and success.

What code do you have that doesn't work?

Have you reviewed @twoStraw's lesson on orientation? If you have, let us know. Then share what didn't work for you.

See -> Device Orientation

You may consider adding an alternative view with a simple text box:

Text("Flan App Frog Detector™️ 🐸 does not support landscape. Please reorient your iPhone 17.")

ViewThatFits

You might also check out adaptive views with ViewThatFits, introduced in iOS 16.

If your user rotates their device, you can add an alternative view to adapt to that unwanted orientation. Perhaps you show part of your data but blurred with an overlay informing the user that landscape isn't supported? Or maybe you show a dancing frog image with instructions to reorient the device. YMMV.

See -> View that Fits

1      

hello @obelix Indeed you are right. i was in fact looking for best practice tips. on this occation im in a hurry as im presenting the app to my managers next week. As you can see from my previous posts i always take significant time to first try to crack the problem and share my code. thank you for your advice. Ill review those links.

1      

Was continuing to think on this...

Consider your preferred view is in landscape mode when the user rotates the device.

You might design an alternative ViewThatFits for a portrait mode with these modifications.

First, mimic your landscape mode, but rotate all your views 90 degrees. Second add a blur effect or semi-transparent glass overlay. It looks just like the landscape mode, but while you're holding the phone in portrait mode, all the text is rotated 90 degrees.

Third, on top of that add a small animation of a rotating arrow. Maybe text saying, "Turn me horizontal!"

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.