16 C
London
Saturday, June 8, 2024

ios – SwiftUI .toolbar not exhibiting at backside of display screen


New coder right here, and I’m studying SwiftUI for the primary time. I began an iOS coding journey this yr after not having coded since 2005, once I final coded a social media web site utilizing HTML, CSS, and Chilly Fusion7. I’ve been following tutorials for some time and can’t get previous this bug that doesn’t generate any errors. I could have embedded the .toolbar within the incorrect space of SwiftUI View. However the toolbar refuses to indicate on the backside of the display screen. See the 2 screenshots. One exhibits the toolbar, however the report title and choose button will not be on the high of the display screen. The opposite is the place I mounted the choose/edit button, however the toolbar is lacking.

Screenshot With Toolbar

Missing Toolbar

import SwiftUI
import PhotosUI

struct ReportDetailView: View {
    @Binding var report: Report
    @State non-public var showPhotoLibrary = false
    @State non-public var showCamera = false
    @State non-public var selectedImages: [UIImage] = []
    @State non-public var selectedImage: UIImage?
    @State non-public var selectedImageIndex: Int?
    @State non-public var showEditCaption = false
    @State non-public var showEmailSheet = false
    @State non-public var isSelecting = false
    @State non-public var selectedPhotos = Set<Int>()

    var physique: some View {
        VStack {
            TextField("Report Title", textual content: Binding(
                get: { report.title ?? "" },
                set: { report.title = $0 }
            ))
            .font(.largeTitle)
            .padding(.high)
            .background(Colour.clear)
            .body(maxWidth: .infinity, alignment: .main)
            .textInputAutocapitalization(.phrases)
            .onTapGesture {
                self.dismissKeyboard()
            }
            .padding(.horizontal)

            if report.images.isEmpty {
                Textual content("No Pictures")
                    .foregroundColor(.grey)
                    .padding()
                    .body(maxWidth: .infinity, maxHeight: .infinity, alignment: .heart)
            } else {
                ScrollView {
                    LazyVGrid(columns: [GridItem(.adaptive(minimum: 100))]) {
                        ForEach(report.images.indices, id: .self) { index in
                            VStack {
                                if let picture = report.images[index].picture {
                                    Picture(uiImage: picture)
                                        .resizable()
                                        .aspectRatio(contentMode: .match)
                                        .onTapGesture {
                                            if isSelecting {
                                                if selectedPhotos.comprises(index) {
                                                    selectedPhotos.take away(index)
                                                } else {
                                                    selectedPhotos.insert(index)
                                                }
                                            } else {
                                                selectedImage = report.images[index].picture
                                                selectedImageIndex = index
                                                showEditCaption = true
                                            }
                                        }
                                        .overlay(
                                            isSelecting ?
                                            Circle()
                                                .stroke(selectedPhotos.comprises(index) ? Colour.blue : Colour.clear, lineWidth: 3)
                                                .body(width: 30, peak: 30)
                                                .padding(5)
                                                .background(Colour.white.opacity(0.7).clipShape(Circle()))
                                                .padding()
                                            : nil
                                        )
                                }
                                Textual content(report.images[index].caption)
                                    .font(.caption)
                            }
                        }
                    }
                }
            }
        }
        .navigationBarItems(trailing: Button(motion: {
            isSelecting.toggle()
            selectedPhotos.removeAll()
        }) {
            Textual content(isSelecting ? "Cancel" : "Edit")
        })
        .toolbar {
            ToolbarItem(placement: .bottomBar) {
                HStack {
                    if isSelecting {
                        Button(motion: deleteSelectedPhotos) {
                            Picture(systemName: "trash")
                                .foregroundColor(.crimson)
                        }
                        Spacer()
                    }
                    
                    Button(motion: { showPhotoLibrary = true }) {
                        Picture(systemName: "picture.on.rectangle.angled")
                    }
                    Spacer()
                    Button(motion: { showCamera = true }) {
                        Picture(systemName: "digicam")
                    }
                    Spacer()
                    Button(motion: { showEmailSheet = true }) {
                        Picture(systemName: "envelope")
                    }
                }
            }
        }
        .sheet(isPresented: $showPhotoLibrary) {
            UnifiedPicker(pickerType: .photoLibrary(single: false), selectedImages: $selectedImages)
                .onDisappear {
                    for picture in selectedImages {
                        if let imageData = picture.jpegData(compressionQuality: 1.0) {
                            report.images.append(Photograph(imageData: imageData, caption: ""))
                        }
                    }
                    selectedImages.removeAll()
                }
        }
        .sheet(isPresented: $showCamera) {
            UnifiedPicker(pickerType: .digicam, selectedImages: $selectedImages)
                .onDisappear {
                    if let picture = selectedImages.first, let imageData = picture.jpegData(compressionQuality: 1.0) {
                        report.images.append(Photograph(imageData: imageData, caption: ""))
                    }
                    selectedImages.removeAll()
                }
        }
        .sheet(isPresented: $showEditCaption) {
            if let selectedIndex = selectedImageIndex {
                EditCaptionView(picture: $report.images[selectedIndex])
            }
        }
        .sheet(isPresented: $showEmailSheet) {
            if let emailData = emailData {
                MailComposeViewController(mailData: emailData)
            }
        }
    }

    non-public func deleteSelectedPhotos() {
        for index in selectedPhotos.sorted(by: >) {
            report.images.take away(at: index)
        }
        selectedPhotos.removeAll()
        isSelecting.toggle()
    }

    non-public func dismissKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }

    non-public var emailData: Knowledge? {
        return PDFGenerator.generatePDF(for: report)
    }
}

struct ReportDetailView_Previews: PreviewProvider {
    @State static var report = Report(title: "Pattern Report", date: Date(), images: [])
    static var previews: some View {
        ReportDetailView(report: $report)
    }
}

I’ve tried shifting the .toolbar into totally different areas of the SwiftUI View, however all I’ve executed is efficiently crash the app and generate errors. This code is as shut as I’ve been capable of get.

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here