- Open your project in Xcode
- Go to 'File' → 'Add Packages...' or select your project in the Editor, go to Package Dependencies, and press the '+'
- Enter the Package URL, which is the GitHub repository URL for this project, https://github.com/Royal-Air-Force/flight-ui-ios
- Select the package
- Select a Dependency Rule, we recommend setting this to 'Up to Next Major Version'
- Click 'Add Package'
- Under 'Add to Target', select your main project target
- Select the additional target to add the library to in the Project Editor
- Go to the 'General' tab
- Scroll to 'Frameworks, Libraries, and Embedded Content'
- Click the '+' button and select the FlightUI package from the list
- Click 'Add'
Our recommended approach to utilising FlightUI is to provide the ThemeManager and Theme object as an Environment variable. This can be achieved by the following steps;
- Create the
ThemeManagerinstance as a State Object within your App Struct. You can provide a customisedThemeobject to the theme manager within it's constructor.
import FlightUI
@main
struct MyApp: App {
@StateObject var themeManager = ThemeManager()
// or
@StateObject var themeManager = ThemeManager(current: .dark)
}
- Provide the
themeManagerandthemeManager.currentas environment objects to your navigation stack, and set the colour scheme environment to match.
var body: some Scene {
WindowGroup {
NavigationStack {
...
}
.environmentObject(themeManager)
.environmentObject(themeManager.current)
.accentColor(themeManager.current.onBackground.default)
.environment(\.colorScheme, themeManager.current.baseScheme)
}
}
- When you need to access elements of FlightUI, ensure it is passed through as an environment object and then access it directly inside your View struct
struct MyView: View {
@EnvironmentObject var theme: Theme
var body: some View {
VStack {
...
}
.background(theme.color.surface)
.padding(.all, theme.padding.grid4x)
}
}
- If you want to change the theme, for example between light and dark, then access your Theme Manager object as an environment object and call the
updateThemefunction
struct ThemeToggleButton: View {
@EnvironmentObject var themeManager: ThemeManager
@EnvironmentObject var theme: Theme
var body: some View {
Button("Light Theme", action: {
themeManager.updateTheme(.light)
})
.buttonStyle(theme.button.filled)
.padding(.all, theme.padding.grid2x)
}
}
- By default, calling
updateThemewill also change the app environment colour scheme to the one defined in yourTheme.baseScheme, if you have customised this or are managing the environment manually, then you can prevent this by settingchangeBaseThemetofalse
...
var body: some View {
Button("Light Theme", action: {
themeManager.updateTheme(.light, changeBaseTheme: false)
})
.buttonStyle(theme.button.filled)
.padding(.all, theme.padding.grid2x)
}