I'm having difficulties aligning the navigation title with my content view in SwiftUI. This operation is trivial to do in UIKit especially using collection view flow layout.
n.b. This is for an experimental project and I'm using iOS 15.
I've tried many different approaches :
1. Using a list
This gives us the right alignment between the navigation title, the header and cells on the iPhone 12 Max but not on the iPhone 12
The scroll view doesn't extend over the edges as it would in the Apple Music app.
Is there a way to fix the scroll view?
iPhone 12 Max screenshot
iPhone 12 Screenshot
code
var body: some View {
NavigationView {
GeometryReader { proxy in
let cellWidth = proxy.size.width * 0.4
List {
ForEach(0...10, id: \.self) { rowIndex in
Section(header: Text("Unknown").font(.title2).redacted(reason: .placeholder)) {
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(alignment: .top, spacing: 16) {
ForEach(0...10, id: \.self) { index in
VStack(alignment: .leading, spacing: 2) {
RoundedRectangle(cornerRadius: 8)
.foregroundColor(.red)
.frame(width: cellWidth, height: cellWidth)
VStack(alignment: .leading, spacing: 2) {
Text("Example")
.foregroundColor(.primary)
.font(.subheadline)
.lineLimit(1)
.redacted(reason: .placeholder)
Text("Example")
.foregroundColor(.secondary)
.font(.subheadline)
.lineLimit(1)
.redacted(reason: .placeholder)
}
}
}
}
}
}
}
}
.listStyle(InsetListStyle())
.navigationBarTitle("Experimental")
}
}
}
2. Using a VStack and padding ...
- There doesn't seem to be a straightforward way to align the
VStack
and first cell with the title. For example on an iPhone 12 Pro Max, a padding of 20 gives the right alignment and on an iPhone 12 we will be off by 2 pixels or so using the same padding. - Now the horizontal scrollviews behave as expected by padding only the first cell.
iPhone 12 Max Screenshot
iPhone 12 Screenshot
code
var bodyUsingVStack: some View {
NavigationView {
GeometryReader { proxy in
let cellWidth = proxy.size.width * 0.4
ScrollView {
VStack(alignment: .leading, spacing: 16) {
ForEach(0...10, id: \.self) { rowIndex in
Section(
header: Text("Unknown")
.font(.title2)
.padding(.leading, 20)
// .redacted(reason: .placeholder)
) {
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(alignment: .top, spacing: 16) {
ForEach(0...10, id: \.self) { index in
VStack(alignment: .leading, spacing: 2) {
RoundedRectangle(cornerRadius: 8)
.foregroundColor(.red)
.frame(width: cellWidth, height: cellWidth)
VStack(alignment: .leading, spacing: 2) {
Text("Example")
.foregroundColor(.primary)
.font(.subheadline)
.lineLimit(1)
.redacted(reason: .placeholder)
Text("Example")
.foregroundColor(.secondary)
.font(.subheadline)
.lineLimit(1)
.redacted(reason: .placeholder)
}
}
.padding(.leading, index == 0 ? 22 : 0)
}
}
}
}
}
}
}
.listStyle(InsetListStyle())
.navigationBarTitle("Experimental")
}
}
}
- Other ideas
- Using alignment guides doesn't seem to work
- Tempted to extend the underlying UINavigation view and capture the exact position of the title to calculate the correct padding.
- Test all iOS devices and predetermine the correct padding value for each device...
There must be a simpler way and it is crazy that SwiftUI allows so much flexibility and yet we often get stuck for hours or days trying to do trivial things.