Swift Form on iOS

I am new to swift (developer from long ago) and have not been able to resolve an issue with IOS - I am building a text entry box with save / cancel buttons at bottom and a texteditor above all within a fixed size frame. It's all OK on macOS but when I try iOS the space taken by the form weirdly expands (beyond the size of the text editor) and end up pushing the save /cancel buttons below outside view. An extract of the code is here. I have tried all sorts of things like setting frame sizes, line limits, spacers, scrolling disable, but I can't stop the behaviour. Any ideas. Apologies if this is well known or my issue due to inexperience I just can't resolve it.

private func editSource(in geometry: GeometryProxy) -> some View {

NavigationStack{ Form { Section(header: Text("Source Name")) { TextEditor(text: $newSourceName) .font(.title3) .padding(5) .background( RoundedRectangle(cornerRadius: 10) .fill(.background) ) .textFieldStyle(PlainTextFieldStyle()) .frame(height: 50) .foregroundColor(.primary) .focused($isSourceFocused) } }

        HStack {
                    Button("Save") {
                        events[eventIndex].source = newSourceName
                        isEditingSource = false
                        isSourceFocused = false
                        disableEdit = false
                    }
                    Spacer()
                    Button("Cancel", role: .cancel) {
                        isSourceFocused = false
                        isEditingSource = false
                        disableEdit = false
                    }
                }
                .padding()
                .layoutPriority(1)
        }
        

#if os(macOS) .scrollDisabled(true) .padding() .frame(height: 180) .frame(width: 240) .background(.gray) #else .frame(width: 240) .frame(height: 240)

#endif .cornerRadius(10) .clipShape(RoundedRectangle(cornerRadius: 10)) .overlay( RoundedRectangle(cornerRadius: 10) .stroke(Color.blue, lineWidth: 1) ) .position(sourceLocation) .offset(x: 0, y: 100) .zIndex(1) .onAppear() { isSourceFocused = true newSourceName = events[eventIndex].source } }

Welcome to the forum.

Before one can answer, please:

  • post complete code so that we can reproduce (not only the editSource func)
  • format thee code with code formatter tool. As you can see, your present code is just unreadable.

+1 to @Claude31 , and also, if you say there's some layout issue, why not include a screenshot so we can see what you mean?

Apologies I wasn't sure what formatter to use - bear with me ? Here are the screen shots showing how the form keeps growing downwards (text editor stays within limits).

Here is some standalone code with same behaviour you can try. I entered as a code block hope this works. Sincerely appreciate any input.

//
//  ContentView.swift
//  TestDataEntry
//

import SwiftUI


struct ContentView: View {
    
    @State private var newSourceName: String = ""
    
    var body: some View {
        editSource()
    }
    
    private func editSource() -> some View {
        NavigationStack{
            Form {
                Section(header: Text("Source Name")) {
                    TextEditor(text: $newSourceName)
                        .font(.title3)
                        .padding(5)
                        .background(
                            RoundedRectangle(cornerRadius: 10)
                                .fill(.background)
                        )
                        .textFieldStyle(PlainTextFieldStyle())
                        .frame(height: 50)
                        .foregroundColor(.primary)
                    
                    
                }
            }
            
            HStack {
                Button("Save") {
                    
                }
                Spacer()
                Button("Cancel", role: .cancel) {
                    
                }
            }
            .padding()
            .layoutPriority(1)
        }
        
        
#if os(macOS)
        .scrollDisabled(true)
        .padding()
        .frame(height: 180)
        .frame(width: 240)
        .background(.gray)
#else
        .frame(width: 240)
        .frame(height: 240)
        
#endif
        .cornerRadius(10)
        .clipShape(RoundedRectangle(cornerRadius: 10))
        .overlay(
            RoundedRectangle(cornerRadius: 10)
                .stroke(Color.blue, lineWidth: 1)
        )
        
        .zIndex(1)
        .onAppear() {
            
        }
    }
}

    struct ContentView_Previews: PreviewProvider {
            static var previews: some View {
                ContentView()
            }
    }
Swift Form on iOS
 
 
Q