Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wvteijlingen/swiftui-interface-orientation
Allow interface orientations on a per view basis in SwiftUI
https://github.com/wvteijlingen/swiftui-interface-orientation
interface orientation swift swiftui
Last synced: 2 months ago
JSON representation
Allow interface orientations on a per view basis in SwiftUI
- Host: GitHub
- URL: https://github.com/wvteijlingen/swiftui-interface-orientation
- Owner: wvteijlingen
- License: mit
- Created: 2023-10-11T18:29:01.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-28T15:17:30.000Z (6 months ago)
- Last Synced: 2024-10-15T19:40:57.157Z (3 months ago)
- Topics: interface, orientation, swift, swiftui
- Language: Swift
- Homepage:
- Size: 16.6 KB
- Stars: 22
- Watchers: 2
- Forks: 3
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# swiftui-interface-orientation
The view modifier that Apple forgot. *SwiftInterfaceOrientation* allows you to easily specify the orientations
that are supported by your SwiftUI views.## Usage
Attach the `.interfaceOrientations` modifier to any SwiftUI view to specify the interface orientations that are
supported by that view. As long as the view is visible, rotation to the specified orientations will be allowed.For example, the following view will be restricted to the `.portrait` orientation, unless the Toggle is switched on:
```swift
import InterfaceOrientationstruct MyView: View {
@State private var isLandscapeAllowed = falsevar body: some View {
Toggle("Allow landscape", isOn: $isLandscapeAllowed)
.interfaceOrientations(isLandscapeAllowed ? [.portrait, .landscape] : .portrait)
}
}
```## Setup
Create an application delegate using `@UIApplicationDelegateAdaptor`, and implement `application(_:supportedInterfaceOrientationsFor:)`:```swift
private class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
InterfaceOrientationCoordinator.shared.supportedOrientations
}
}
```## Multiple views
If orientations are specified by multiple views, the supported orientations for the app are defined by the lowest
common denominator (i.e. the intersection of all specified orientations).For example, given the following code, the only allowed orientation is `.portrait` because that is
the only orientation which is supported by all views:```swift
VStack {
A().interfaceOrientations([.portrait, .landscape])
B().interfaceOrientations([.portrait, .portraitUpsideDown])
}
```## Default orientations
When there are no views that specify custom orientations, a set of default orientations will be used.
*SwiftInterfaceOrientation* attempts to read this set from the Info.plist, but you can set it manually using
`InterfaceOrientationCoordinator.shared.defaultOrientations`### Overriding default orientations
Views are allowed to support orientations that are not present in the set of default orientations.
This means that if a view supports `.landscapeLeft`, the interface can rotate to landscape, even when
the default orientations don't include `.landscapeLeft`.## iPad
When running on iPad, *SwiftInterfaceOrientation* needs some extra configuration to work. You need to do one of the following:
1. Set UIRequiresFullScreen to YES in the Info.plist
2. Make sure that *not all* the orientations are selected in the Deployment Info for the project. For example, only select 'Portrait'.
You can still specify and support other orientations using the view modifier.