1

I want make different background colors for the top and bottom of a list.

When I use UIKIT I Do like this link

Now I use SwiftUI. And I have no idea how can I make this..

thanks!

5
  • Good question, I remember trying and failing to do this before Commented Jun 27, 2021 at 4:04
  • do u mean different colors for multiple rows? Commented Jun 27, 2021 at 4:25
  • @JaisonThomas No, I'm not. I want different list background colors Commented Jun 27, 2021 at 4:50
  • just to make sure [link] (stackoverflow.com/a/56523389) isn't what you're looking for ? Commented Jun 27, 2021 at 4:57
  • @JaisonThomas Sorry. No. It's not what I want I want like this link Commented Jun 27, 2021 at 5:01

1 Answer 1

2

You can add a UIView to the header. I have found a height of 400 is large enough to cover the pull down without seeing the top edge.

 var body: some View {
        
        NavigationView {
            
            List(data, id: \.self) { data in
                Text("\(data)")
            }
            .onAppear {
                let headerView = UIView(frame: CGRect(x: 0, y: -400, width: UIScreen.main.bounds.width, height: 400.0))
                headerView.backgroundColor = .lightGray
                UITableView.appearance().addSubview(headerView)
            }
            .navigationBarTitle("Title", displayMode: .inline)
        }
    }

enter image description here


If you want a different background color below the List then add another UIView to change the backgroundView:

let backgroundView = UIView()
backgroundView.backgroundColor = .black
UITableView.appearance().backgroundView = backgroundView

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.