Screen Background Color With ScrollView And Navigation Bar SwiftUI

后端 未结 2 1499
情话喂你
情话喂你 2020-12-16 21:14

Im trying to have a colored background while using a scrollview with SwiftUI but this causes the navigation title to no longer collapse. I\'ve tried a number of ways yet thi

2条回答
  •  渐次进展
    2020-12-16 21:42

    Here is the solution I came up with... this solution also allows you to colour the background cells a different colour to your List Elements. You could do a very similar thing with ScrollView

    As mentioned in the comments for the other solution, carrying out that solution with ListView leaves a bunch of whitespace from the cells. (Which is why this is useful)

    I thought I would add to this as none of the other solutions worked for me.

    struct ListBackgroundColor: ViewModifier {
    
        let color: UIColor
    
        func body(content: Content) -> some View {
            content
                .onAppear() {
                    UITableView.appearance().backgroundColor = self.color
                    //(Optional) Edit colour of cell background
                    UITableViewCell.appearance().backgroundColor = self.color
                }
        }
    }   
    
    extension View {
        func listBackgroundColor(color: UIColor) -> some View {
            ModifiedContent(content: self, modifier: ListBackgroundColor(color: color))
        }
    
    }
    

    UI Example https://imgur.com/a/TQ9c5Sc

提交回复
热议问题