Home > AI > IOS > SwiftUI >

HStack equally divide subviews

Example 1:

struct ContentView: View {
    @EnvironmentObject var home: HomeGlobal
    
    var body: some View {        
        HStack(spacing: 0) {
            Text("本周新增")
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(Color.blue)
            
            Text("高频")
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(Color.red)
            
            Text("近日新增")
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(Color.green)
            
            Text("命中率")
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(Color.yellow)
        }
    }
}

Example 2: with divider

struct ContentView: View {
    @EnvironmentObject var home: HomeGlobal
    
    var body: some View {
        
        HStack(spacing: 0) {
            Text("本周新增")
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(Color.blue)
            
            Rectangle()
                .frame(width: 1, height: 10)
                .foregroundColor(.gray)
            
            Text("高频")
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(Color.red)
            
            Rectangle()
                .frame(width: 1, height: 10)
                .foregroundColor(.gray)
            
            Text("近日新增")
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(Color.green)
            
            Rectangle()
                .frame(width: 1, height: 10)
                .foregroundColor(.gray)
            
            Text("命中率")
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(Color.yellow)
        }
    }
}

Leave a Reply