首页 > 解决方案 > 聊天视图中的白色矩形空间

问题描述

我有一个简单的聊天应用程序,当我运行我的项目时,会出现类似白色矩形的图像。我检查了我的代码,我没有检测到任何错误。当我添加工具栏时,这个矩形填充底部,有什么想法吗?

截屏: 在此处输入图像描述

聊天视图:

   struct chatList : View {

   var body : some View{
    
    ScrollView(.vertical, showsIndicators: false) {
        
        VStack{
            
            ForEach(Eachmsg){i in
                
                chatCell(data: i)
            }
        }
        
    }.padding(.horizontal, 15)
    .background(Color.white)
    
  }}

struct chatCell : View {

var data : msgdataType

var body : some View{
    
    HStack{
        
        if data.myMsg{
            
            Spacer()
            
            Text(data.msg)
                .padding()
                .background(Color("color"))
                .clipShape(msgTail(mymsg: data.myMsg))
                .foregroundColor(.white)
        }
        else{
            
            Text(data.msg)
                .padding()
                .background(Color("gray"))
                .clipShape(msgTail(mymsg: data.myMsg))
            
            Spacer()
        }
        
    }.padding(data.myMsg ? .leading : .trailing, 55)
    .padding(.vertical,10)
    }
    }

消息数据类型:

struct msgdataType : Identifiable {
var id : Int
var msg : String
var myMsg : Bool
}

每个消息:

 var Eachmsg = [

msgdataType(id: 0, msg: "New Album Is Going To Be Released!!!!", 
myMsg: false),.....

标签: swiftswiftui

解决方案


在您的 chatCell 中,显示的空白区域用于消息myMsg=true

if data.myMsg {
        Spacer()
        Text(data.msg)
            .padding()
            .background(Color("color"))
            .clipShape(msgTail(mymsg: data.myMsg))
            .foregroundColor(.white)
    }

尝试更改foregroundColor.red或 。blue或更改background为有效值,您将看到该消息。


推荐阅读