Dynamic header title #15

Open
opened 2025-10-14 17:31:02 -06:00 by navan · 0 comments
Owner

Originally created by @ibohonos on 5/21/2020

Hello!

if dynamic header title, page scroll not work
How fix this?

There is examle:

struct TestView: View {
    var body: some View {
        NavigationView {
            VStack {
                List {
                    NavigationLink(destination: GalleryView()) {
                        Text("Gallery View")
                    }
                }
            }
        }
        .navigationBarTitle(Text(""), displayMode: .inline)
    }
}

struct GalleryView: View {

    @State var index: Int = 0
    
    func setupTitle(forPage page: Int) -> String {
        switch page {
            case 0: return "Extended"
            case 1: return "PrePay"
            case 2: return "Disabled"
            default: return "Extended \(page)"
        }
    }

    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("My Art Collection")
                .font(.system(size: 40, weight: .bold))
                .padding([.horizontal, .top])
            ModelPages(paintings, currentPage: $index, hasControl: false) { i, painting in
                PaintingView(painting: painting)
            }
            Spacer()
        }
        .navigationBarTitle(Text(setupTitle(forPage: index)), displayMode: .inline)
    }

}

private struct PaintingView: View {

    var painting: Painting

    @State var list = ["One", "Two", "Three", "Four", "Five", "Six", "Seven"]

    var body: some View {
        VStack {
            Image(painting.image)
                .resizable()
                .scaledToFit()
            VStack(alignment: .leading) {
                Text(painting.title)
                    .font(.system(size: 30, weight: .bold))
                Text(painting.author)
                    .foregroundColor(.secondary)
            }
            List {
                ForEach(list, id: \.self) {
                    Text($0)
                }
                .onMove { self.list.move(fromOffsets: $0, toOffset: $1) }
            }
            Button(action: {}) {
                HStack {
                    Spacer()
                    Text("Buy for $\(painting.price)")
                        .fontWeight(.semibold)
                        .foregroundColor(.white)
                        .padding(.vertical)
                    Spacer()
                }
                .background(Color.blue)
                .cornerRadius(10)
            }
            .padding(.top, 30)
            Spacer()
        }
        .padding(.horizontal)
        .environment(\.editMode, .constant(.active))
    }
}
*Originally created by @ibohonos on 5/21/2020* Hello! if dynamic header title, page scroll not work How fix this? There is examle: ``` swift struct TestView: View { var body: some View { NavigationView { VStack { List { NavigationLink(destination: GalleryView()) { Text("Gallery View") } } } } .navigationBarTitle(Text(""), displayMode: .inline) } } struct GalleryView: View { @State var index: Int = 0 func setupTitle(forPage page: Int) -> String { switch page { case 0: return "Extended" case 1: return "PrePay" case 2: return "Disabled" default: return "Extended \(page)" } } var body: some View { VStack(alignment: .leading, spacing: 8) { Text("My Art Collection") .font(.system(size: 40, weight: .bold)) .padding([.horizontal, .top]) ModelPages(paintings, currentPage: $index, hasControl: false) { i, painting in PaintingView(painting: painting) } Spacer() } .navigationBarTitle(Text(setupTitle(forPage: index)), displayMode: .inline) } } private struct PaintingView: View { var painting: Painting @State var list = ["One", "Two", "Three", "Four", "Five", "Six", "Seven"] var body: some View { VStack { Image(painting.image) .resizable() .scaledToFit() VStack(alignment: .leading) { Text(painting.title) .font(.system(size: 30, weight: .bold)) Text(painting.author) .foregroundColor(.secondary) } List { ForEach(list, id: \.self) { Text($0) } .onMove { self.list.move(fromOffsets: $0, toOffset: $1) } } Button(action: {}) { HStack { Spacer() Text("Buy for $\(painting.price)") .fontWeight(.semibold) .foregroundColor(.white) .padding(.vertical) Spacer() } .background(Color.blue) .cornerRadius(10) } .padding(.top, 30) Spacer() } .padding(.horizontal) .environment(\.editMode, .constant(.active)) } } ```
Sign in to join this conversation.
No labels
enhancement
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github/Pages#15
No description provided.