简体   繁体   English

RoundedRectangle 背景色不裁剪和 TextEditor 透明背景

[英]RoundedRectangle background colour does not crop and TextEditor transparent background

I have a messaging interface.我有一个消息传递界面。 When user types in to the texteditor it will be append to messagesDBArray and will be displayed in textview .当用户在texteditor编辑器中输入时,它将是 append 到messagesDBArray并将显示在textview中。 Once new messages are there it should scroll to the bottom.一旦有新消息,它应该滚动到底部。 But I'm having issues.但我有问题。

Errors: no errors错误:没有错误

  1. RoundedRectangle background colour green overflows from corners (does not crop as rounded) RoundedRectangle背景颜色绿色从角落溢出(不crop为圆形)
  2. TextEditor (not textview) is not transparent (so it can have rounded rectangle color underneath) TextEditor (不是 textview)不是transparent的(所以它下面可以有圆角矩形颜色)
  3. proxy.scrollTo(id, anchor: .bottom) does not scrolls to the last message. proxy.scrollTo(id, anchor: .bottom)不会滚动到最后一条消息。
import SwiftUI
    
final class ViewModel: ObservableObject {
    @Published var messagesDBArray : [SingleMessageBubbleModel] = []
}
    
struct SingleMessageBubbleModel: Identifiable {
    let id = UUID()
    var text: String
    var received: Bool
    var timeStamp: Date
}
    
var messagesDBArray : [SingleMessageBubbleModel] = []
    
struct ContentView: View {
    @ObservedObject private var messageArrayObservedObject = ViewModel()
        
    @State private var showOnTheSpotMessaging: Bool = true
    @State var textTyped: String = ""
        
    var body: some View {
    VStack (alignment: .center) {    
        ZStack (alignment: .center) {      
            HStack () {
                RoundedRectangle(cornerRadius: 25, style: .continuous)
                    .stroke(Color.brown, lineWidth: 1)
                    .frame(width: 300, alignment: Alignment.top )
                    .padding([.bottom], 5)
                    .clipped()
                    .background(Color.green)        
            }
            HStack () {
                ScrollViewReader { proxy in
                    ScrollView {
                        LazyVStack {
                            ForEach(
                                messageArrayObservedObject.messagesDBArray,
                                id: \.id
                            ) {
                                message in MessageBubble(message: message)
                            }
                        }
                    }
                    .frame(alignment: .center)
                    .background(Color.clear)
                    .padding (.vertical, 5)
                    .padding (.horizontal,5)
                    .padding(.bottom, 5)
                    
                    .onChange(
                        of: messageArrayObservedObject.messagesDBArray.count
                    ) { id in
                        // When the lastMessageId changes, scroll to the bottom of the conversation
                        withAnimation {
                            proxy.scrollTo(id, anchor: .bottom)
                        }
                    }
                }
                .frame( height: 200, alignment: .center)
            }
            .frame(width: 295, alignment: Alignment.center )  
        } 
        HStack () {
            VStack {    
                ZStack (alignment: .center) {
                    HStack () {
                        RoundedRectangle(cornerRadius: 25, style: .continuous)
                            .stroke(Color.brown , lineWidth: 1)
                            .frame(width: 295, alignment: Alignment.top )
                            .padding([.bottom], 5)
                            .clipped()
                            .background(Color.green)
                            // .background(Color("#E5F2E4"))
                    }
                    HStack () {
                        TextEditor (text: $textTyped)
                            .frame(height: 200, alignment: .leading)
                            .padding(.horizontal, 10)
                            .background(.clear)
                        }
                    }
                    .frame(width: 290, alignment: Alignment.top )
                    .padding(.top, 5)
                }
            }   
        }
    }
    struct MessageBubble: View {
        var message: SingleMessageBubbleModel
        @State private var showTime = false
        
        var body: some View {
            VStack(alignment: message.received ? .leading : .trailing) {
                HStack {
                    Text(message.text)
                        .padding()
                        .background(message.received ? Color.gray : Color.blue)
                        .cornerRadius(30)
                }
                .frame(maxWidth: 300, alignment: message.received ? .leading : .trailing)
                .onTapGesture {
                    withAnimation {
                    showTime.toggle()
                }
            }  
        }
        .frame(maxWidth: .infinity, alignment: message.received ? .leading : .trailing)
        .padding(message.received ? .leading : .trailing)
        .padding(.horizontal, 4)
    }
}

for the first error you should use that code instead of your code where you make a background with RoundRectangle the same to your base rectangle and make the fill of that the color you want which is green对于第一个错误,您应该使用该代码而不是您的代码,在该代码中,您使用与基本矩形相同的 RoundRectangle 制作背景,并填充您想要的颜色,即绿色

              RoundedRectangle(cornerRadius: 25, style: .continuous)
                    .stroke(Color.brown, lineWidth: 1).background(RoundedRectangle(cornerRadius: 25).fill(Color.green))
                     .frame(width: 300, alignment: Alignment.top )
                     .padding([.bottom], 5)
                     .clipped()

the second issue in your ContentView you should init your UITextView background color to clear and after that make your textEditor Color clear using that code ContentView 中的第二个问题,您应该初始化 UITextView 背景颜色以清除,然后使用该代码清除 textEditor Color

    init() {
       UITextView.appearance().backgroundColor = .clear
   }

and make your textEditor background clear并使您的文本编辑器背景清晰

TextEditor (text: $textTyped)
                .frame(height: 200, alignment: .leading)
                .padding(.horizontal, 10)
                .background(Color.clear)

and the third issue is that i think you are using the array count but you should use the id of each message so when if we suppose that the last message-id is 728398 in your onChange第三个问题是,我认为您使用的是数组计数,但您应该使用每条消息的 ID,所以如果我们假设最后一条消息 ID 在您的 onChange 中是728398

onChange(of: messageArrayObservedObject.messagesDBArray.count) { id in
                // When the lastMessageId changes, scroll to the bottom of the conversation
                withAnimation {
                    print("ididididid\(id)")
                    proxy.scrollTo(messageArrayObservedObject.messagesDBArray.last, anchor: .bottom)
                }
            }

your are using the ( messageArrayObservedObject.messagesDBArray.count )counts of messages like 5 message so you are scrolling to 5 not to the id of message which is 728398您正在使用( messageArrayObservedObject.messagesDBArray.count )条消息计数,例如5 条消息,因此您滚动到5而不是消息的 id,即728398

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM