I've created an HStack and am trying to make one of those titles that you find in Apple apps (Messages, Mail, etc.) at the top left. I've gotten the text to the right size, made it bold, and aligned to the left and used .frame(alignment: .top) to align it to the top, but for some reason it stays in the middle. What am I doing wrong? Below is the code and an image of the issue.
import SwiftUI
struct HomeScreen: View {
var body: some View {
HStack{
Text("Tasks").font(.largeTitle).fontWeight(.bold).frame(maxWidth: .infinity, alignment: .leading).padding()
.frame(alignment: .top)
}
}
struct HomeScreen_Previews: PreviewProvider {
static var previews: some View {
HomeScreen()
}
}
}

Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
This is a weird but simple issue that I'm having and I'm hoping someone can figure it out: I have a modal that contains a form and a NavigationView for the navigationTitle and navigationBarItems. However, the form doesn't take up the whole modal, leaving this weird white gap between the navigationBar and form. Is there any way to remove this weird gap? Attached is my code. Thanks!
var body: some View {
// Create Navigation View for the title at the top
NavigationView {
VStack(spacing: 0) {
Text("")
// Navigation Title
.navigationTitle("Modal Title").navigationBarTitleDisplayMode(.inline)
// Creating Cancel Button
.navigationBarItems(leading: Button(action: {
self.isPresented = false
}, label: {
Text("Cancel").frame(maxWidth: .infinity, alignment: .leading)
.frame(alignment: .top)}))
// Creating Add button
.navigationBarItems(trailing: Button(action: {
self.isPresented = false
print("\(self.taskName)")
print("\(self.description)")
}, label: {
Text("Add").frame(maxWidth: .infinity, alignment: .leading)
.frame(alignment: .top)}))
// Creating form for the text fields
Form {
// Section 1: Title and Desctiption text views
Section {
TextField("Title", text: $taskName)
TextField("Description", text: $description)
.ignoresSafeArea()
}
Section {
DatePicker("Date", selection: $date, in: Date()..., displayedComponents: .date)
.datePickerStyle(GraphicalDatePickerStyle())
}
}
}
}
}
I'm in the process of building a SwiftUI app with a Sidebar and Detail View. I've run into some issues and I need some help fixing them:
When the app is launched, my detail view shows up at the right of the sidebar. Great! However, the button that is supposed to navigate to that view isn't highlighted, which could cause user confusion. How do I make this button "light up" (with that blue background indicating to the user that it is selected, highlighted) and make the code so that this view opens up when the app opens (like in other Apple apps, see Music and Files)
When I click on one of my sidebar items (which are just NavigationLinks), the background doesn't turn blue (highlight) to indicate that item is selected. I feel like this would cause user confusion, and I'd like to figure out why my code doesn't do this. One side note and a useful piece of information: whenever I click the NavigationLink in the sidebar, the console prints this: onChange(of: UpdateTrigger) action tried to update multiple times per frame.
In the macOS app, upon launch, the detail view shows up. Great. What doesn't show up is my sidebar with the options on it. How do I make it so that the sidebar shows up no matter what unless the user specifies to remove it from view?
Attached are some images and my code. Thanks, y'all!
struct ContentView: View {
var body: some View {
// NavigationSplitView for the sidebar
NavigationSplitView {
// List of the options
List {
// NavigationLink for my button
NavigationLink {
// Linking to my main view that I want to show up upon launch of the app
MainView()
// Label to make it look pretty
} label: {
Label("Main View", systemImage: "icloud")
}
// Make the sidebar actually look like a sidebar
.listStyle(.sidebar)
}
// NavigationTitle at the top of the sidebar
.navigationTitle("Cling")
//
} detail: {
// Make it so that the main screen shows up upon launch (however, this doesn't make the button light up to signify that the user has selected that view)
MainView()
}
}
}
This is a bit of a weird one, but I’m having an issue where when I run my SwiftUI app on Mac to test (pressing command + r pr pressing the play button, previews work fine), it opens a blank window without any content in it. If I open a new window by pressing command + n, the actual window of the app shows up. What’s weirder is that this only happens on one Mac; I’ve tried it on another one with the same Xcode project, same code, same OS version, same Xcode version, etc, and the issue doesn’t occur; the app opens the normal window as expected. It also doesn’t happen on iOS and iPadOS, both on devices or in the simulator.
I’m thinking it might be some cache files that messed something up and are preventing the right window from loading, but unlike the iOS simulator, I can’t delete the app and rerun to “reinstall.” Does anyone know how to do this?
Any help would be greatly appreciated. Super annoying and certainly not shippable; I don’t want to use up one of my TSIs to figure it out since nobody else on the entire internet has had this issue. Maybe it’s just a SwiftUI bug? I don’t know! I’ve attached some code below and a screen shot
of the issue. Thanks!
// Main app file
@main
struct CitationsApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
// ContentView
struct ContentView: View {
@State private var isShowingMLAView = true
var body: some View {
NavigationView {
List {
NavigationLink(destination: MLA().navigationTitle("Create Citation"), isActive: $isShowingMLAView) {
Label("MLA", systemImage: "doc.text")}
}
.listStyle(.sidebar)
.navigationTitle("Citations")
}
}
}
I’ve been trying really hard to get to try NavigationSplitView in my app, but I’m really confused as to how to use it when I’m not passing values in my app. I just have bindings. I really want to use the new features that come with the new API, but I just can’t figure out how. Attached is what I’m doing now, any help figuring out how to do this exact thing with the new NavigationSplitView API would be greatly appreciated.
// ContentView
var body: some View {
NavigationView {
Sidebar() .navigationTitle("Formats")
Detail()
}
}
// Sidebar
@State private var isShowingDetail = true
var body: some View {
List {
NavigationLink(destination: Detail().navigationTitle(“Detail"), isActive: $isShowingMLAView) {
Label(“Detail", systemImage: “ellipsis.circle")}
// Other Navigation Links here...
I’ll preface this by saying I’ve submitted a DTS ticket with essentially this exact text, but I thought I’d also post about it here to get some additional input. Apple engineers: the case ID is 14698374, for your reference.
I have an observable class, called NavigationModel, that powers navigation in my SwiftUI app. It has one important property, navigationSelection, that stores the currently selected view. This property is passed to a List in the sidebar column of a NavigationSplitView with two columns. The list has NavigationLinks that accept that selection as a value parameter. When a NavigationLink is tapped, the detail column shows the appropriate detail view per the navigationSelection property’s current value via a switch statement. (This navigationSelection stores an enum value.)
This setup allows for complete programmatic navigation as that selection is effectively global. From anywhere in the app — any button, command, or app intent — the selection can be modified since the NavigationModel class uses the @Observable Swift macro. In the app’s root file, an instance of the NavigationModel is created, added as an app intent dependency, and assigned (might be the wrong verb here, but you get it) to ContentView, which houses the NavigationSplitView code.
The problem lies when more than one window is opened. Because this is all just one instance of NavigationModel — initialized in the app’s root file — the navigation selection is shared across windows. That is, there is no way for one window to show a view and another to show another view — they’re bound to the same instance of NavigationModel. Again, this was done so that app intents and menu bar commands can modify the navigation selection, but this causes unintended behavior.
I checked Apple’s sample projects, namely the “Accelerating app interactions with App Intents” (https://developer.apple.com/documentation/appintents/acceleratingappinteractionswithappintents) and “Adopting App Intents to support system experiences” (https://developer.apple.com/documentation/appintents/adopting-app-intents-to-support-system-experiences) projects, to see how Apple recommends handling this case. Both of these projects have intents to display a view by modifying the navigation selection. They also have the same unintended behavior I’m experiencing in my app. If two windows are open, they share the navigation selection.
I feel pretty stupid asking for help with this, but I’ve tried a lot to get it to work the way I want it to. My first instinct was to create a new instance of NavigationModel for each window, and that’s about 90% of the way there, but app intents fail when there are no open windows because there are no instances of NavigationModel to modify. I also tried playing with FocusedValue and SceneStorage, but those solutions also didn’t play well with app intents and added too much convoluted code for what should be a simple issue.
In total, what I want is:
A window/scene-specific navigation selection property that works across TabViews and NavigationSplitViews
A way to reliably modify that property’s value across the app for the currently focused window
A way to set a value as a default, so when the app launches with a window, it automatically selects a value in the detail column
The navigation selection to reset across app and window launches, restoring the default selection
Does anyone know how to do this? I’ve scoured the internet, but again, no dice. Usually Apple’s sample projects are great with this sort of thing, but all of their projects that have scene-specific navigation selection with NavigationSplitView don’t have app intents. 🤷♂️
If anyone needs additional code samples, I’d be happy to provide them, but it’s basically a close copy of Apple’s own sample code found in those two links.