skip to Main Content

I have a static table (or Form in SwiftUI) I created in SwiftUI, I’m happy with how every is working except for the line separating the two static cells. See image below.

enter image description here

I’ve tried a few methods, but nothing seems to work. You can see them commented out in there.

struct Settings_CheckedView: View {
    
    @Environment(.managedObjectContext) private var viewContext
    @Environment(.settings) private var settings
    
    @State private var picker1 = 2
    @State private var picker2 = 2
    
    //Tried this first, doesn't seem to affect it.
    /*init() {
        UITableView.appearance().separatorStyle = .none
    }*/
    
    var body: some View {
        Form {
            Section(header: Text("COMPLETED ITEM BEHAVIOR")) {
                VStack {
                    HStack {
                        Text("Auto-Delete Completed Items")
                        Spacer()
                    }
                    Picker("", selection: $picker1) {
                        Text("Off").tag(0)
                        Text("Immediately").tag(1)
                        Text("Delay").tag(2)
                    }
                    .pickerStyle(SegmentedPickerStyle())
                    .onChange(of: picker1, perform: { value in
                        settings.autoDeleteSegment = Int16(value)
                        viewContext.trySave("Updating auto-delete segment value in Settings", loc: "Settings_CheckedView")
                    })
                }
                VStack {
                    HStack {
                        Text("Delay Time Interval")
                        Spacer()
                    }
                    Picker("", selection: $picker2) {
                        Text("5 mins").tag(0)
                        Text("15 mins").tag(1)
                        Text("1 Hr").tag(2)
                        Text("24 Hrs").tag(3)
                    }
                    .pickerStyle(SegmentedPickerStyle())
                    .onChange(of: picker2, perform: { value in
                        settings.deleteDelaySegment = Int16(value)
                        self.viewContext.trySave("Updating delay time interval segment value in Settings", loc: "Settings_CheckedView")
                    })
                }
                .isHidden(picker1 == 2 ? false: true)
            }
        }
        .navigationBarTitle(Text("Completed Items"))
        .navigationBarColor(UIColor.ezDarkAquaSet)
        .onAppear(perform: {
            self.picker1 = Int(settings.autoDeleteSegment)
            self.picker2 = Int(settings.deleteDelaySegment)
            
            //Tried this second, also no affect
            //UITableView.appearance().separatorStyle = .none
        })
        .listSeparatorStyle(style: .none) //Last try, also no affect, see code for this below.
    }
}

extension View {
    func listSeparatorStyle(style: UITableViewCell.SeparatorStyle) -> some View {
        ModifiedContent(content: self, modifier: ListSeparatorStyle(style: style))
    }
}

struct ListSeparatorStyle: ViewModifier {
    let style: UITableViewCell.SeparatorStyle
    
    func body(content: Content) -> some View {
        content
            .onAppear() {
                UITableView.appearance().separatorStyle = self.style
            }
    }
}

So you can see basically top to bottom what I tried but nothing was working. They are all basically different ways to implement the .separatorStyle. When I would try doing stuff like this in UIKit and this was called a static UITableView, this is exactly what I would do to remove it. But I’m not sure what I’m doing wrong here. All of the examples I found online were in reference to a List instead of a Form so perhaps this is why it’s not working. Any suggestions?

2

Answers


  1. Simply wrap the 2 VStacks inside the Section with another VStack and the line will disappear

        ...
        Section(header: Text("COMPLETED ITEM BEHAVIOR")) {
            VStack {
                VStack {
                    HStack {
                        Text("Auto-Delete Completed Items")
                        Spacer()
                    }
                    ...
                }
                VStack {
                    HStack {
                        Text("Delay Time Interval")
                        Spacer()
                    }
                    ...
                }
            }
        }
        ...
    
    Login or Signup to reply.
  2. Add .listRowSeparator(.hidden) to each component

     TextField("Enter first name", text: $firstName)
         .textFieldStyle(.roundedBorder)
         .listRowSeparator(.hidden)
     TextField("Enter last name", text: $lastName)
         .textFieldStyle(.roundedBorder)
         .listRowSeparator(.hidden)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search