(SwiftUI) 如何读取响应式视图的几何?

问题描述

enter image description here

我需要在此聊天气泡视图之上添加一个视图,例如 Instagram 表情符号反应。我们称之为 EmojiView。它需要始终位于聊天气泡的左下角或右下角。但是聊天气泡的大小会随着其中的文本数量而变化。所以我需要根据 EmojiView 上面气泡的大小来定位它。

但是我没有明确的方法来读取气泡的大小。 GeometryReader 占用了所有可用空间,这在这种情况下是一个问题。如果我将文本包裹在 GeometryReader 中,它将占用所有高度空间,并且我没有可靠的高度空间可供读取。如果我用 .aspectRatio(.fit) 修改 GeometryReader,高度就等于宽度。如果我将任何地方设置为 .fixedSize() ,聊天气泡将不再适应文本大小。所以请分享一个可靠的方法来阅读这个几何

请帮忙解决这个问题

没有 GeometryReader,我无法读取几何

使用 GeometryReader,我无法获得可靠的高度读数

解决方法

无需使用 GeometryReader


使用 ZStack 并将 EmojiView 对齐在 bottomTrailingbottomLeading 上,具体取决于您要显示它的底部。

            ZStack(alignment: .bottomLeading) {
                HStack {
                    Text("I need to add a view on top of this chat bubble view,like an Instagram emoji reaction. Let's call it EmojiView. It will need to always be in the bottom left or right corner of the chat bubble. But the chat bubble's size will vary with how much text is in it. So I need to position EmojiView based on the size of the bubble it is on top of.")
                        .background(Color.secondary)
                        .padding()
                }
                Text("RABBITS")
                    .padding(8)
                    .background(Color.blue)
                    .padding(20)
            }