0

我尝试了所有方法,但 .lineLimit(nil)、.fixedSize(horizo​​ntal: true, vertical: false)、.fixedSize(horizo​​ntal: false, vertical: true) 不起作用,我不知道如何将文本放在下一行,如果它走出屏幕。文本是按钮,按钮位于列表中。

func showTime(hm: HourAndMinute) -> some View{
    
    return HStack {
        Text(String(hm.hour)).bold()
        ForEach(hm.minute, id: \.self) { minute in
            
            Button(action: {
                
                print(hm.minute)
                
            }) {
                Text(String(minute)).frame(width: 30, height: 25).background(Color("hour")).cornerRadius(5)
                
            }.buttonStyle(BorderlessButtonStyle())
            
        }
    }.fixedSize(horizontal: true, vertical: false)
}

在此处输入图像描述

4

2 回答 2

1

如果您不想错过任何框,可以尝试使用 ScrollView。

ScrollView(.horizontal) {
    HStack {
        Text(String(hm.hour)).bold()
        ForEach(hm.minute, id: \.self) { minute in                    
            Button(action: {
                print(hm.minute)                
            }) {
                Text(String(perc))
                   .frame(width: 30, height: 25)
                   .background(Color("ora")).cornerRadius(5)
            
            }.buttonStyle(BorderlessButtonStyle())            
        }
    }
}

另一方面,如果你想剪辑容器视图,你可以使用:

.clipShape(Rectangle())
于 2020-11-24T23:07:27.360 回答
0

我找到了答案:LazyVGrid

获取屏幕宽度:

let singleWidth:CGFloat = UIScreen.main.bounds.width

网格项目布局:

var gridItemLayout = Array(repeating: GridItem(.fixed(30), spacing: 10), count: Int(singleWidth/50))

return LazyVGrid (columns: gridItemLayout, spacing: 10) {

最终解决方案:

let singleWidth:CGFloat = UIScreen.main.bounds.width

func showTime(hm: HourAndMinute) -> some View{
var gridItemLayout = Array(repeating: GridItem(.fixed(30), spacing: 10), count: Int(singleWidth/50))
        
        return  LazyVGrid (columns: gridItemLayout, spacing: 10) {
        Text(String(hm.hour)).bold()
        ForEach(hm.minute, id: \.self) { minute in
            
            Button(action: {
                
                print(hm.minute)
                
            }) {
                Text(String(minute)).frame(width: 30, height: 25).background(Color("hour")).cornerRadius(5)
                
            }.buttonStyle(BorderlessButtonStyle())
            
        }
    }
}
于 2020-11-25T15:29:24.600 回答